Reversible Computation in Term Rewriting111This work has been partially supported by the EU (FEDER) and the Spanish Ministerio de Economía y Competitividad (MINECO) under grants TIN2013-44742-C4-1-R and TIN2016-76843-C4-1-R, by the Generalitat Valenciana under grant PROMETEO-II/2015/013 (SmartLogic), and by the COST Action IC1405 on Reversible Computation - extending horizons of computing. Adrián Palacios was partially supported by the EU (FEDER) and the Spanish Ayudas para contratos predoctorales para la formación de doctores and Ayudas a la movilidad predoctoral para la realización de estancias breves en centros de I+D, MINECO (SEIDI), under FPI grants BES-2014-069749 and EEBB-I-16-11469. Part of this research was done while the second and third authors were visiting Nagoya University; they gratefully acknowledge their hospitality.
© 2017. This manuscript version is made available under the CC-BY-NC-ND 4.0 license http://creativecommons.org/licenses/by-nc-nd/4.0/

Naoki Nishida nishida@i.nagoya-u.ac.jp Adrián Palacios apalacios@dsic.upv.es Germán Vidal gvidal@dsic.upv.es Graduate School of Informatics, Nagoya University
Furo-cho, Chikusa-ku, 4648603 Nagoya, Japan
MiST, DSIC, Universitat Politècnica de València
Camino de Vera, s/n, 46022 Valencia, Spain
Abstract

Essentially, in a reversible programming language, for each forward computation from state S𝑆S to state Ssuperscript𝑆S^{\prime}, there exists a constructive method to go backwards from state Ssuperscript𝑆S^{\prime} to state S𝑆S. Besides its theoretical interest, reversible computation is a fundamental concept which is relevant in many different areas like cellular automata, bidirectional program transformation, or quantum computing, to name a few.

In this work, we focus on term rewriting, a computation model that underlies most rule-based programming languages. In general, term rewriting is not reversible, even for injective functions; namely, given a rewrite step t1t2subscript𝑡1subscript𝑡2t_{1}\to t_{2}, we do not always have a decidable method to get t1subscript𝑡1t_{1} from t2subscript𝑡2t_{2}. Here, we introduce a conservative extension of term rewriting that becomes reversible. Furthermore, we also define two transformations, injectivization and inversion, to make a rewrite system reversible using standard term rewriting. We illustrate the usefulness of our transformations in the context of bidirectional program transformation.

To appear in the Journal of Logical and Algebraic Methods in Programming.

keywords:
term rewriting , reversible computation , program transformation

1 Introduction

The notion of reversible computation can be traced back to Landauer’s pioneering work [22]. Although Landauer was mainly concerned with the energy consumption of erasing data in irreversible computing, he also claimed that every computer can be made reversible by saving the history of the computation. However, as Landauer himself pointed out, this would only postpone the problem of erasing the tape of a reversible Turing machine before it could be reused. Bennett [6] improved the original proposal so that the computation now ends with a tape that only contains the output of a computation and the initial source, thus deleting all remaining “garbage” data, though it performs twice the usual computation steps. More recently, Bennett’s result is extended in [9] to nondeterministic Turing machines, where it is also proved that transforming an irreversible Turing machine into a reversible one can be done with a quadratic loss of space. We refer the interested reader to, e.g., [7, 14, 40] for a high level account of the principles of reversible computation.

In the last decades, reversible computing and reversibilization (transforming an irreversible computation device into a reversible one) have been the subject of intense research, giving rise to successful applications in many different fields, e.g., cellular automata [28], where reversibility is an essential property, bidirectional program transformation [24], where reversibility helps to automate the generation of inverse functions (see Section 6), reversible debugging [17], where one can go both forward and backward when seeking the cause of an error, parallel discrete event simulation [34], where reversible computation is used to undo the effects of speculative computations made on a wrong assumption, quantum computing [39], where all computations should be reversible, and so forth. The interested reader can find detailed surveys in the state of the art reports of the different working groups of COST Action IC1405 on Reversible Computation [20].

In this work, we introduce reversibility in the context of term rewriting [4, 36], a computation model that underlies most rule-based programming languages. In contrast to other, more ad-hoc approaches, we consider that term rewriting is an excellent framework to rigorously define reversible computation in a functional context and formally prove its main properties. We expect our work to be useful in different (sequential) contexts, like reversible debugging, parallel discrete event simulation or bidirectional program transformation, to name a few. In particular, Section 6 presents a first approach to formalize bidirectional program transformation in our setting.

To be more precise, we present a general and intuitive notion of reversible term rewriting by defining a Landauer embedding. Given a rewrite system {\mathcal{R}} and its associated (standard) rewrite relation subscript\to_{\mathcal{R}}, we define a reversible extension of rewriting with two components: a forward relation subscript\mathrel{\rightharpoonup_{\mathcal{R}}} and a backward relation subscript\mathrel{\leftharpoondown_{\mathcal{R}}}, such that subscript\mathrel{\rightharpoonup_{\mathcal{R}}} is a conservative extension of subscript\to_{\mathcal{R}} and, moreover, ()1=superscriptsubscript1subscript\mathrel{(\rightharpoonup_{\mathcal{R}})^{-1}}\,=\,\mathrel{\leftharpoondown_{\mathcal{R}}}. We note that the inverse rewrite relation, ()1superscriptsubscript1\mathrel{(\to_{\mathcal{R}})^{-1}}, is not an appropriate basis for “reversible” rewriting since we aim at defining a technique to undo a particular reduction. In other words, given a rewriting reduction stsubscriptsuperscript𝑠𝑡s\to^{\ast}_{\mathcal{R}}t, our reversible relation aims at computing the term s𝑠s from t𝑡t and {\mathcal{R}} in a decidable and deterministic way, which is not possible using ()1superscriptsubscript1\mathrel{(\to_{\mathcal{R}})^{-1}} since it is generally non-deterministic, non-confluent, and non-terminating, even for systems defining injective functions (see Example 6). In contrast, our backward relation subscript\leftharpoondown_{\mathcal{R}} is deterministic (thus confluent) and terminating. Moreover, our relation proceeds backwards step by step, i.e., the number of reduction steps in stsubscriptsuperscript𝑠𝑡s\rightharpoonup^{\ast}_{\mathcal{R}}t and tssubscriptsuperscript𝑡𝑠t\leftharpoondown^{\ast}_{\mathcal{R}}s are the same.

In order to introduce a reversibilization transformation for rewrite systems, we use a flattening transformation so that the reduction at top positions of terms suffices to get a normal form in the transformed systems. For instance, given the following rewrite system:

𝖺𝖽𝖽(𝟢,y)y,𝖺𝖽𝖽(𝗌(x),y)𝗌(𝖺𝖽𝖽(x,y))missing-subexpression𝖺𝖽𝖽0𝑦𝑦missing-subexpressionmissing-subexpression𝖺𝖽𝖽𝗌𝑥𝑦𝗌𝖺𝖽𝖽𝑥𝑦missing-subexpression\begin{array}[]{lrcll}&\mathsf{add}(\mathsf{0},y)&\to&y,{}\\ &\mathsf{add}(\mathsf{s}(x),y)&\to&\mathsf{s}(\mathsf{add}(x,y))&\\ \end{array}

defining the addition on natural numbers built from constructors 𝟢0\mathsf{0} and 𝗌()𝗌\mathsf{s}(~{}), we produce the following flattened (conditional) system:

={𝖺𝖽𝖽(𝟢,y)y,𝖺𝖽𝖽(𝗌(x),y)𝗌(z)𝖺𝖽𝖽(x,y)z}\begin{array}[]{lrcll}{\mathcal{R}}=\{&\mathsf{add}(\mathsf{0},y)&\to&y,\\ &\mathsf{add}(\mathsf{s}(x),y)&\to&\mathsf{s}(z)\Leftarrow\mathsf{add}(x,y)\twoheadrightarrow z&\}\\ \end{array}

(see Example 29 for more details). This allows us to provide an improved notion of reversible rewriting in which some information (namely, the positions where reduction takes place) is not required anymore. This opens the door to compile the reversible extension of rewriting into the system rules. Loosely speaking, given a system {\mathcal{R}}, we produce new systems fsubscript𝑓{\mathcal{R}}_{f} and bsubscript𝑏{\mathcal{R}}_{b} such that standard rewriting in fsubscript𝑓{\mathcal{R}}_{f}, i.e., fsubscriptsubscript𝑓\to_{{\mathcal{R}}_{f}}, coincides with the forward reversible extension subscript\rightharpoonup_{\mathcal{R}} in the original system, and analogously bsubscriptsubscript𝑏\to_{{\mathcal{R}}_{b}} is equivalent to subscript\leftharpoondown_{\mathcal{R}}. E.g., for the system {\mathcal{R}} above, we would produce

f={𝖺𝖽𝖽𝚒(𝟢,y)y,β1,𝖺𝖽𝖽𝚒(𝗌(x),y)𝗌(z),β2(w)𝖺𝖽𝖽𝚒(x,y)z,w}b={𝖺𝖽𝖽1(y,β1)𝟢,y,𝖺𝖽𝖽1(𝗌(z),β2(w))𝗌(x),y𝖺𝖽𝖽1(z,w)x,y}\begin{array}[]{l@{~}r@{~}c@{~}l@{~}l}{\mathcal{R}}_{f}=\{\hfil~{}&\mathsf{add}^{\mathtt{i}}(\mathsf{0},y)~{}&\to\hfil~{}&\langle y,\beta_{1}\rangle,\hfil~{}\\ \hfil~{}&\mathsf{add}^{\mathtt{i}}(\mathsf{s}(x),y)~{}&\to\hfil~{}&\langle\mathsf{s}(z),\beta_{2}(w)\rangle\Leftarrow\mathsf{add}^{\mathtt{i}}(x,y)\twoheadrightarrow\langle z,w\rangle\hfil~{}&\}\\[8.61108pt] {\mathcal{R}}_{b}=\{\hfil~{}&\mathsf{add}^{-1}(y,\beta_{1})~{}&\to\hfil~{}&\langle\mathsf{0},y\rangle,\hfil~{}\\ \hfil~{}&\mathsf{add}^{-1}(\mathsf{s}(z),\beta_{2}(w))~{}&\to\hfil~{}&\langle\mathsf{s}(x),y\rangle\Leftarrow\mathsf{add}^{-1}(z,w)\to\langle x,y\rangle\hfil~{}&\}\end{array}

where 𝖺𝖽𝖽𝚒superscript𝖺𝖽𝖽𝚒\mathsf{add}^{\mathtt{i}} is an injective version of function 𝖺𝖽𝖽𝖺𝖽𝖽\mathsf{add}, 𝖺𝖽𝖽1superscript𝖺𝖽𝖽1\mathsf{add}^{-1} is the inverse of 𝖺𝖽𝖽𝚒superscript𝖺𝖽𝖽𝚒\mathsf{add}^{\mathtt{i}}, and β1,β2subscript𝛽1subscript𝛽2\beta_{1},\beta_{2} are fresh symbols introduced to label the rules of {\mathcal{R}}.

In this work, we will mostly consider conditional rewrite systems, not only to have a more general notion of reversible rewriting, but also to define a reversibilization technique for unconditional rewrite systems, since the application of flattening (cf. Section 4) may introduce conditions in a system that is originally unconditional, as illustrated above.

This paper is an extended version of [31]. In contrast to [31], our current paper includes the proofs of technical results, the reversible extension of term rewriting is introduced first in the unconditional case (which is simpler and more intuitive), and presents an improved injectivization transformation when the system includes injective functions. Furthermore, a prototype implementation of the reversibilization technique is publicly available from http://kaz.dsic.upv.es/rev-rewriting.html.

The paper is organized as follows. After introducing some preliminaries in Section 2, we present our approach to reversible term rewriting in Section 3. Section 4 introduces the class of pure constructor systems where all reductions take place at topmost positions, so that storing this information in reversible rewrite steps becomes unnecessary. Then, Section 5 presents injectivization and inversion transformations in order to make a rewrite system reversible with standard rewriting. Here, we also present an improvement of the transformation for injective functions. The usefulness of these transformations is illustrated in Section 6. Finally, Section 7 discusses some related work and Section 8 concludes and points out some ideas for future research.

2 Preliminaries

We assume familiarity with basic concepts of term rewriting. We refer the reader to, e.g., [4] and [36] for further details.

2.1 Terms and Substitutions

A signature {\mathcal{F}} is a set of ranked function symbols. Given a set of variables 𝒱𝒱{\mathcal{V}} with 𝒱=𝒱{\mathcal{F}}\cap{\mathcal{V}}=\varnothing, we denote the domain of terms by 𝒯(,𝒱)𝒯𝒱{\mathcal{T}}({\mathcal{F}},{\mathcal{V}}). We use 𝖿,𝗀,𝖿𝗀\mathsf{f},\mathsf{g},\ldots to denote functions and x,y,𝑥𝑦x,y,\ldots to denote variables. Positions are used to address the nodes of a term viewed as a tree. A position p𝑝p in a term t𝑡t, in symbols p𝒫𝗈𝗌(t)𝑝𝒫𝗈𝗌𝑡p\in\mathsf{{{\cal P}}os}(t), is represented by a finite sequence of natural numbers, where ϵitalic-ϵ\epsilon denotes the root position. We let t|pevaluated-at𝑡𝑝t|_{p} denote the subterm of t𝑡t at position p𝑝p and t[s]p𝑡subscriptdelimited-[]𝑠𝑝t[s]_{p} the result of replacing the subterm t|pevaluated-at𝑡𝑝t|_{p} by the term s𝑠s. 𝒱ar(t)𝒱𝑎𝑟𝑡{\mathcal{V}ar}(t) denotes the set of variables appearing in t𝑡t. We also let 𝒱ar(t1,,tn)𝒱𝑎𝑟subscript𝑡1subscript𝑡𝑛{\mathcal{V}ar}(t_{1},\ldots,t_{n}) denote 𝒱ar(t1)𝒱ar(tn)𝒱𝑎𝑟subscript𝑡1𝒱𝑎𝑟subscript𝑡𝑛{\mathcal{V}ar}(t_{1})\cup\cdots\cup{\mathcal{V}ar}(t_{n}). A term t𝑡t is ground if 𝒱ar(t)=𝒱𝑎𝑟𝑡{\mathcal{V}ar}(t)=\varnothing.

A substitution σ:𝒱𝒯(,𝒱):𝜎maps-to𝒱𝒯𝒱\sigma:{\mathcal{V}}\mapsto{\mathcal{T}}({\mathcal{F}},{\mathcal{V}}) is a mapping from variables to terms such that 𝒟𝗈𝗆(σ)={x𝒱xσ(x)}𝒟𝗈𝗆𝜎conditional-set𝑥𝒱𝑥𝜎𝑥\mathsf{{{\mathcal{D}}}om}(\sigma)=\{x\in{\mathcal{V}}\mid x\neq\sigma(x)\} is its domain. A substitution σ𝜎\sigma is ground if xσ𝑥𝜎x\sigma is ground for all x𝒟𝗈𝗆(σ)𝑥𝒟𝗈𝗆𝜎x\in\mathsf{{{\mathcal{D}}}om}(\sigma). Substitutions are extended to morphisms from 𝒯(,𝒱)𝒯𝒱{\mathcal{T}}({\mathcal{F}},{\mathcal{V}}) to 𝒯(,𝒱)𝒯𝒱{\mathcal{T}}({\mathcal{F}},{\mathcal{V}}) in the natural way. We denote the application of a substitution σ𝜎\sigma to a term t𝑡t by tσ𝑡𝜎t\sigma rather than σ(t)𝜎𝑡\sigma(t). The identity substitution is denoted by id𝑖𝑑id. We let “\circ” denote the composition of substitutions, i.e., σθ(x)=(xθ)σ=xθσ𝜎𝜃𝑥𝑥𝜃𝜎𝑥𝜃𝜎\sigma\circ\theta(x)=(x\theta)\sigma=x\theta\sigma. The restriction θ|`V\theta\!\mathrel{|\grave{}}_{V} of a substitution θ𝜃\theta to a set of variables V𝑉V is defined as follows: xθ|`V=xθx\theta\!\mathrel{|\grave{}}_{V}=x\theta if xV𝑥𝑉x\in V and xθ|`V=xx\theta\!\mathrel{|\grave{}}_{V}=x otherwise.

2.2 Term Rewriting Systems

A set of rewrite rules lr𝑙𝑟l\to r such that l𝑙l is a nonvariable term and r𝑟r is a term whose variables appear in l𝑙l is called a term rewriting system (TRS for short); terms l𝑙l and r𝑟r are called the left-hand side and the right-hand side of the rule, respectively. We restrict ourselves to finite signatures and TRSs. Given a TRS {\mathcal{R}} over a signature {\mathcal{F}}, the defined symbols 𝒟subscript𝒟{\mathcal{D}}_{\mathcal{R}} are the root symbols of the left-hand sides of the rules and the constructors are 𝒞=\𝒟subscript𝒞\subscript𝒟{\mathcal{C}}_{\mathcal{R}}={\mathcal{F}}\;\backslash\;{\mathcal{D}}_{\mathcal{R}}. Constructor terms of {\mathcal{R}} are terms over 𝒞subscript𝒞{\mathcal{C}}_{\mathcal{R}} and 𝒱𝒱{\mathcal{V}}, denoted by 𝒯(𝒞,𝒱)𝒯subscript𝒞𝒱{\mathcal{T}}({\mathcal{C}}_{\mathcal{R}},{\mathcal{V}}). We sometimes omit {\mathcal{R}} from 𝒟subscript𝒟{\mathcal{D}}_{\mathcal{R}} and 𝒞subscript𝒞{\mathcal{C}}_{\mathcal{R}} if it is clear from the context. A substitution σ𝜎\sigma is a constructor substitution (of {\mathcal{R}}) if xσ𝒯(𝒞,𝒱)𝑥𝜎𝒯subscript𝒞𝒱x\sigma\in{\mathcal{T}}({\mathcal{C}}_{\mathcal{R}},{\mathcal{V}}) for all variables x𝑥x.

For a TRS {\mathcal{R}}, we define the associated rewrite relation subscript\to_{\mathcal{R}} as the smallest binary relation on terms satisfying the following: given terms s,t𝒯(,𝒱)𝑠𝑡𝒯𝒱s,t\in{\mathcal{T}}({\mathcal{F}},{\mathcal{V}}), we have stsubscript𝑠𝑡s\to_{\mathcal{R}}t iff there exist a position p𝑝p in s𝑠s, a rewrite rule lr𝑙𝑟l\to r\in{\mathcal{R}}, and a substitution σ𝜎\sigma such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma and t=s[rσ]p𝑡𝑠subscriptdelimited-[]𝑟𝜎𝑝t=s[r\sigma]_{p}; the rewrite step is sometimes denoted by sp,lrtsubscript𝑝𝑙𝑟𝑠𝑡s\to_{p,l\to r}t to make explicit the position and rule used in this step. The instantiated left-hand side lσ𝑙𝜎l\sigma is called a redex. A term s𝑠s is called irreducible or in normal form with respect to a TRS {\mathcal{R}} if there is no term t𝑡t with stsubscript𝑠𝑡s\to_{\mathcal{R}}t. A substitution is called normalized with respect to {\mathcal{R}} if every variable in the domain is replaced by a normal form with respect to {\mathcal{R}}. We sometimes omit “with respect to {\mathcal{R}}” if it is clear from the context. A derivation is a (possibly empty) sequence of rewrite steps. Given a binary relation \to, we denote by superscript\rightarrow^{\ast} its reflexive and transitive closure, i.e., stsubscriptsuperscript𝑠𝑡s\to^{\ast}_{\mathcal{R}}t means that s𝑠s can be reduced to t𝑡t in {\mathcal{R}} in zero or more steps; we also use sntsubscriptsuperscript𝑛𝑠𝑡s\to^{n}_{\mathcal{R}}t to denote that s𝑠s can be reduced to t𝑡t in exactly n𝑛n steps.

We further assume that rewrite rules are labelled, i.e., given a TRS {\mathcal{R}}, we denote by β:lr:𝛽𝑙𝑟\beta:l\to r a rewrite rule with label β𝛽\beta. Labels are unique in a TRS. Also, to relate label β𝛽\beta to fixed variables, we consider that the variables of the rewrite rules are not renamed222This will become useful in the next section where the reversible extension of rewriting keeps a “history” of a computation in the form of a list of terms β(p,σ)𝛽𝑝𝜎\beta(p,\sigma), and we want the domain of σ𝜎\sigma to be a subset of the left-hand side of the rule labelled with β𝛽\beta. and that the reduced terms are always ground. Equivalently, one could require terms to be variable disjoint with the variables of the rewrite system, but we require groundness for simplicity. We often write sp,βtsubscript𝑝𝛽𝑠𝑡s\to_{p,\beta}t instead of sp,lrtsubscript𝑝𝑙𝑟𝑠𝑡s\to_{p,l\to r}t if rule lr𝑙𝑟l\to r is labeled with β𝛽\beta.

2.3 Conditional Term Rewrite Systems

In this paper, we also consider conditional term rewrite systems (CTRSs); namely oriented 3-CTRSs, i.e., CTRSs where extra variables are allowed as long as 𝒱ar(r)𝒱ar(l)𝒱ar(C)𝒱𝑎𝑟𝑟𝒱𝑎𝑟𝑙𝒱𝑎𝑟𝐶{\mathcal{V}ar}(r)\subseteq{\mathcal{V}ar}(l)\cup{\mathcal{V}ar}(C) for any rule lrC𝑙𝑟𝐶l\to r\Leftarrow C [26]. In oriented CTRSs, a conditional rule lrC𝑙𝑟𝐶l\to r\Leftarrow C has the form lrs1t1,,sntnformulae-sequence𝑙𝑟subscript𝑠1subscript𝑡1subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow s_{1}\twoheadrightarrow t_{1},\ldots,s_{n}\twoheadrightarrow t_{n}, where each oriented equation sitisubscript𝑠𝑖subscript𝑡𝑖s_{i}\twoheadrightarrow t_{i} is interpreted as reachability (superscriptsubscript\to_{\mathcal{R}}^{\ast}). In the following, we denote by on¯¯subscript𝑜𝑛\overline{o_{n}} a sequence of elements o1,,onsubscript𝑜1subscript𝑜𝑛o_{1},\ldots,o_{n} for some n𝑛n. We also write oi,j¯¯subscript𝑜𝑖𝑗\overline{o_{i,j}} for the sequence oi,,ojsubscript𝑜𝑖subscript𝑜𝑗o_{i},\ldots,o_{j} when ij𝑖𝑗i\leq j (and the empty sequence otherwise). We write o¯¯𝑜\overline{o} when the number of elements is not relevant. In addition, we denote a condition o1o1,,ononformulae-sequencesubscript𝑜1subscriptsuperscript𝑜1subscript𝑜𝑛subscriptsuperscript𝑜𝑛o_{1}\twoheadrightarrow o^{\prime}_{1},\ldots,o_{n}\twoheadrightarrow o^{\prime}_{n} by onon¯¯subscript𝑜𝑛subscriptsuperscript𝑜𝑛\overline{o_{n}\twoheadrightarrow o^{\prime}_{n}}.

As in the unconditional case, we consider that rules are labelled and that labels are unique in a CTRS. And, again, to relate label β𝛽\beta to fixed variables, we consider that the variables of the conditional rewrite rules are not renamed and that the reduced terms are always ground.

For a CTRS {\mathcal{R}}, the associated rewrite relation subscript\to_{\mathcal{R}} is defined as the smallest binary relation satisfying the following: given ground terms s,t𝒯()𝑠𝑡𝒯s,t\in{\mathcal{T}}({\mathcal{F}}), we have stsubscript𝑠𝑡s\to_{\mathcal{R}}t iff there exist a position p𝑝p in s𝑠s, a rewrite rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a ground substitution σ𝜎\sigma such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma, siσtiσsubscriptsuperscriptsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎s_{i}\sigma\to^{\ast}_{\mathcal{R}}t_{i}\sigma for all i=1,,n𝑖1𝑛i=1,\ldots,n, and t=s[rσ]p𝑡𝑠subscriptdelimited-[]𝑟𝜎𝑝t=s[r\sigma]_{p}.

In order to simplify the presentation, we only consider deterministic CTRSs (DCTRSs), i.e., oriented 3-CTRSs where, for each rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}, we have 𝒱𝖺𝗋(si)𝒱𝖺𝗋(l,ti1¯)𝒱𝖺𝗋subscript𝑠𝑖𝒱𝖺𝗋𝑙¯subscript𝑡𝑖1\mathsf{{{\mathcal{V}}}ar}(s_{i})\subseteq\mathsf{{{\mathcal{V}}}ar}(l,\overline{t_{i-1}}) for all i=1,,n𝑖1𝑛i=1,\ldots,n (see Section 3.2 for a justification of this requirement and how it could be relaxed to arbitrary 3-CTRSs). Intuitively speaking, the use of DCTRs allows us to compute the bindings for the variables in the condition of a rule in a deterministic way. E.g., given a ground term s𝑠s and a rule β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}} with s|p=lθevaluated-at𝑠𝑝𝑙𝜃s|_{p}=l\theta, we have that s1θsubscript𝑠1𝜃s_{1}\theta is ground. Therefore, one can reduce s1θsubscript𝑠1𝜃s_{1}\theta to some term s1subscriptsuperscript𝑠1s^{\prime}_{1} such that s1subscriptsuperscript𝑠1s^{\prime}_{1} is an instance of t1θsubscript𝑡1𝜃t_{1}\theta with some ground substitution θ1subscript𝜃1\theta_{1}. Now, we have that s2θθ1subscript𝑠2𝜃subscript𝜃1s_{2}\theta\theta_{1} is ground and we can reduce s2θθ1subscript𝑠2𝜃subscript𝜃1s_{2}\theta\theta_{1} to some term s2subscriptsuperscript𝑠2s^{\prime}_{2} such that s2subscriptsuperscript𝑠2s^{\prime}_{2} is an instance of t2θθ1subscript𝑡2𝜃subscript𝜃1t_{2}\theta\theta_{1} with some ground substitution θ2subscript𝜃2\theta_{2}, and so forth. If all equations in the condition hold using θ1,,θnsubscript𝜃1subscript𝜃𝑛\theta_{1},\ldots,\theta_{n}, we have that sp,βs[rσ]psubscript𝑝𝛽𝑠𝑠subscriptdelimited-[]𝑟𝜎𝑝s\to_{p,\beta}s[r\sigma]_{p} with σ=θθ1θn𝜎𝜃subscript𝜃1subscript𝜃𝑛\sigma=\theta\theta_{1}\ldots\theta_{n}.

Example 1

Consider the following DCTRS {\mathcal{R}} that defines the function 𝖽𝗈𝗎𝖻𝗅𝖾𝖽𝗈𝗎𝖻𝗅𝖾\mathsf{double} that doubles the value of its argument when it is an even natural number:

β1:𝖺𝖽𝖽(𝟢,y)yβ4:𝖾𝗏𝖾𝗇(𝟢)𝗍𝗋𝗎𝖾β2:𝖺𝖽𝖽(𝗌(x),y)𝗌(𝖺𝖽𝖽(x,y))β5:𝖾𝗏𝖾𝗇(𝗌(𝗌(x)))𝖾𝗏𝖾𝗇(x)β3:𝖽𝗈𝗎𝖻𝗅𝖾(x)𝖺𝖽𝖽(x,x)𝖾𝗏𝖾𝗇(x)𝗍𝗋𝗎𝖾:subscript𝛽1absent𝖺𝖽𝖽0𝑦𝑦:subscript𝛽4absent𝖾𝗏𝖾𝗇0𝗍𝗋𝗎𝖾:subscript𝛽2absent𝖺𝖽𝖽𝗌𝑥𝑦𝗌𝖺𝖽𝖽𝑥𝑦:subscript𝛽5absent𝖾𝗏𝖾𝗇𝗌𝗌𝑥𝖾𝗏𝖾𝗇𝑥:subscript𝛽3absent𝖽𝗈𝗎𝖻𝗅𝖾𝑥𝖺𝖽𝖽𝑥𝑥𝖾𝗏𝖾𝗇𝑥𝗍𝗋𝗎𝖾missing-subexpressionmissing-subexpressionmissing-subexpressionmissing-subexpression\begin{array}[]{l@{~}r@{~}c@{~}l@{}l@{~}r@{~}c@{~}l}\beta_{1}:\hfil~{}&\mathsf{add}(\mathsf{0},y)~{}&\to\hfil~{}&y&\beta_{4}:\hfil~{}&\mathsf{even}(\mathsf{0})~{}&\to\hfil~{}&\mathsf{true}\\ \beta_{2}:\hfil~{}&\mathsf{add}(\mathsf{s}(x),y)~{}&\to\hfil~{}&\mathsf{s}(\mathsf{add}(x,y))&\beta_{5}:\hfil~{}&\mathsf{even}(\mathsf{s}(\mathsf{s}(x)))~{}&\to\hfil~{}&\mathsf{even}(x)\\ \beta_{3}:\hfil~{}&\mathsf{double}(x)~{}&\to\hfil~{}&\mathsf{add}(x,x)\Leftarrow\mathsf{even}(x)\twoheadrightarrow\mathsf{true}\\ \end{array}

Given the term 𝖽𝗈𝗎𝖻𝗅𝖾(𝗌(𝗌(𝟢)))𝖽𝗈𝗎𝖻𝗅𝖾𝗌𝗌0\mathsf{double}(\mathsf{s}(\mathsf{s}(\mathsf{0}))) we have, for instance, the following derivation:

𝖽𝗈𝗎𝖻𝗅𝖾(𝗌(𝗌(𝟢)))ϵ,β3𝖺𝖽𝖽(𝗌(𝗌(𝟢)),𝗌(𝗌(𝟢)))since𝖾𝗏𝖾𝗇(𝗌(𝗌(𝟢)))𝗍𝗋𝗎𝖾withσ={x𝗌(𝗌(𝟢))}ϵ,β2𝗌(𝖺𝖽𝖽(𝗌(𝟢),𝗌(𝗌(𝟢))))withσ={x𝗌(𝟢),y𝗌(𝗌(𝟢))}1,β2𝗌(𝗌(𝖺𝖽𝖽(𝟢,𝗌(𝗌(𝟢)))))withσ={x𝟢,y𝗌(𝗌(𝟢))}1.1,β1𝗌(𝗌(𝗌(𝗌(𝟢))))withσ={y𝗌(𝗌(𝟢))}𝖽𝗈𝗎𝖻𝗅𝖾𝗌𝗌0subscriptitalic-ϵsubscript𝛽3𝖺𝖽𝖽𝗌𝗌0𝗌𝗌0superscriptsubscriptsince𝖾𝗏𝖾𝗇𝗌𝗌0𝗍𝗋𝗎𝖾missing-subexpressionmissing-subexpressionmissing-subexpressionmissing-subexpressionwith𝜎maps-to𝑥𝗌𝗌0missing-subexpressionmissing-subexpressionsubscriptitalic-ϵsubscript𝛽2𝗌𝖺𝖽𝖽𝗌0𝗌𝗌0with𝜎formulae-sequencemaps-to𝑥𝗌0maps-to𝑦𝗌𝗌0missing-subexpressionmissing-subexpressionsubscript1subscript𝛽2𝗌𝗌𝖺𝖽𝖽0𝗌𝗌0with𝜎formulae-sequencemaps-to𝑥0maps-to𝑦𝗌𝗌0missing-subexpressionmissing-subexpressionsubscript1.1subscript𝛽1𝗌𝗌𝗌𝗌0with𝜎maps-to𝑦𝗌𝗌0missing-subexpression\begin{array}[]{l@{}l@{}lll}\mathsf{double}(\mathsf{s}(\mathsf{s}(\mathsf{0})))&\to_{\epsilon,\beta_{3}}&\mathsf{add}(\mathsf{s}(\mathsf{s}(\mathsf{0})),\mathsf{s}(\mathsf{s}(\mathsf{0})))&\mbox{since}~{}\mathsf{even}(\mathsf{s}(\mathsf{s}(\mathsf{0})))\to_{\mathcal{R}}^{\ast}\mathsf{true}\\ &&&\mbox{with}~{}\sigma=\{x\mapsto\mathsf{s}(\mathsf{s}(\mathsf{0}))\}\\ &\to_{\epsilon,\beta_{2}}&\mathsf{s}(\mathsf{add}(\mathsf{s}(\mathsf{0}),\mathsf{s}(\mathsf{s}(\mathsf{0}))))&\mbox{with}~{}\sigma=\{x\mapsto\mathsf{s}(\mathsf{0}),~{}y\mapsto\mathsf{s}(\mathsf{s}(\mathsf{0}))\}\\ &\to_{1,\beta_{2}}&\mathsf{s}(\mathsf{s}(\mathsf{add}(\mathsf{0},\mathsf{s}(\mathsf{s}(\mathsf{0})))))&\mbox{with}~{}\sigma=\{x\mapsto\mathsf{0},y\mapsto\mathsf{s}(\mathsf{s}(\mathsf{0}))\}\\ &\to_{1.1,\beta_{1}}&\mathsf{s}(\mathsf{s}(\mathsf{s}(\mathsf{s}(\mathsf{0}))))&\mbox{with}~{}\sigma=\{y\mapsto\mathsf{s}(\mathsf{s}(\mathsf{0}))\}\end{array}

3 Reversible Term Rewriting

In this section, we present a conservative extension of the rewrite relation which becomes reversible. In the following, we use subscript\rightharpoonup_{\mathcal{R}} to denote our reversible (forward) term rewrite relation, and subscript\leftharpoondown_{\mathcal{R}} to denote its application in the reverse (backward) direction. Note that, in principle, we do not require =1subscriptsuperscriptsubscript1\leftharpoondown_{\mathcal{R}}\>=\>\rightharpoonup_{\mathcal{R}}^{-1}, i.e., we provide independent (constructive) definitions for each relation. Nonetheless, we will prove that =1subscriptsuperscriptsubscript1\leftharpoondown_{\mathcal{R}}\>=\>\rightharpoonup_{\mathcal{R}}^{-1} indeed holds (cf. Theorems 9 and 20). In some approaches to reversible computing, both forward and backward relations should be deterministic. Here, we will only require deterministic backward steps, while forward steps might be non-deterministic, as it is often the case in term rewriting.

3.1 Unconditional Term Rewrite Systems

We start with unconditional TRSs since it is conceptually simpler and thus will help the reader to better understand the key ingredients of our approach. In the next section, we will consider the more general case of DCTRSs.

Given a TRS {\mathcal{R}}, reversible rewriting is defined on pairs t,π𝑡𝜋\langle t,\pi\rangle, where t𝑡t is a ground term and π𝜋\pi is a trace (the “history” of the computation so far). Here, a trace in {\mathcal{R}} is a list of trace terms of the form β(p,σ)𝛽𝑝𝜎\beta(p,\sigma) such that β𝛽\beta is a label for some rule lr𝑙𝑟l\to r\in{\mathcal{R}}, p𝑝p is a position, and σ𝜎\sigma is a substitution with 𝒟𝗈𝗆(σ)=𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma)=\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r) which will record the bindings of erased variables when 𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r)\neq\varnothing (and σ=id𝜎𝑖𝑑\sigma=id if 𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)=\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r)=\varnothing).333Note that if a rule lr𝑙𝑟l\to r is non-erasing, i.e., 𝒱𝖺𝗋(l)=𝒱𝖺𝗋(r)𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{V}}}ar}(l)=\mathsf{{{\mathcal{V}}}ar}(r), then σ=id𝜎𝑖𝑑\sigma=id. Our trace terms have some similarities with proof terms [36]. However, proof terms do not store the bindings of erased variables (and, to the best of our knowledge, they are only defined for unconditional TRSs, while we use trace terms both for unconditional and conditional TRSs).

Our reversible term rewriting relation is only defined on safe pairs:

Definition 2

Let {\mathcal{R}} be a TRS. The pair s,π𝑠𝜋\langle s,\pi\rangle is safe in {\mathcal{R}} iff, for all β(p,σ)𝛽𝑝𝜎\beta(p,\sigma) in π𝜋\pi, σ𝜎\sigma is a ground substitution with 𝒟𝗈𝗆(σ)=𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma)=\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r) and β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}}.

In the following, we often omit {\mathcal{R}} when referring to traces and safe pairs if the underlying TRS is clear from the context.

Safety is not necessary when applying a forward reduction step, but will become essential for the backward relation subscript\leftharpoondown_{\mathcal{R}} to be correct. E.g., all traces that come from the forward reduction of some initial pair with an empty trace will be safe (see below). Reversible rewriting is then introduced as follows:

Definition 3

Let {\mathcal{R}} be a TRS. A reversible rewrite relation subscript\rightharpoonup_{\mathcal{R}} is defined on safe pairs t,π𝑡𝜋\langle t,\pi\rangle, where t𝑡t is a ground term and π𝜋\pi is a trace in {\mathcal{R}}. The reversible rewrite relation extends standard rewriting as follows:444In the following, we consider the usual infix notation for lists where [][\,] is the empty list and x:xs:𝑥𝑥𝑠x:xs is a list with head x𝑥x and tail xs𝑥𝑠xs.

s,πt,β(p,σ):π\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}\langle t,\beta(p,\sigma^{\prime}):\pi\rangle

iff there exist a position p𝒫𝗈𝗌(s)𝑝𝒫𝗈𝗌𝑠p\in\mathsf{{{\cal P}}os}(s), a rewrite rule β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}}, and a ground substitution σ𝜎\sigma such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma, t=s[rσ]p𝑡𝑠subscriptdelimited-[]𝑟𝜎𝑝t=s[r\sigma]_{p}, and σ=σ|`𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r)}. The reverse relation, subscript\leftharpoondown_{\mathcal{R}}, is then defined as follows:

t,β(p,σ):πs,π\langle t,\beta(p,\sigma^{\prime}):\pi\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle

iff t,β(p,σ):πdelimited-⟨⟩:𝑡𝛽𝑝superscript𝜎𝜋\langle t,\beta(p,\sigma^{\prime}):\pi\rangle is a safe pair in {\mathcal{R}} and there exist a ground substitution θ𝜃\theta and a rule β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}} such that 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜃𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r), t|p=rθevaluated-at𝑡𝑝𝑟𝜃t|_{p}=r\theta and s=t[lθσ]p𝑠𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s=t[l\theta\sigma^{\prime}]_{p}. Note that θσ=σθ=θσ𝜃superscript𝜎superscript𝜎𝜃𝜃superscript𝜎\theta\sigma^{\prime}=\sigma^{\prime}\theta=\theta\cup\sigma^{\prime}, where \cup is the union of substitutions, since 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜃𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r), 𝒟𝗈𝗆(σ)=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r))𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r)) and both substitutions are ground, so 𝒟𝗈𝗆(θ)𝒟𝗈𝗆(σ)=𝒟𝗈𝗆𝜃𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)\cap\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\varnothing.

We denote the union of both relations subscriptsubscript\rightharpoonup_{\mathcal{R}}\cup\leftharpoondown_{\mathcal{R}} by \rightleftharpoons_{\mathcal{R}}.

Example 4

Let us consider the following TRS {\mathcal{R}} defining the addition on natural numbers built from 𝟢0\mathsf{0} and 𝗌()𝗌\mathsf{s}(~{}), and the function 𝖿𝗌𝗍𝖿𝗌𝗍\mathsf{fst} that returns its first argument:

β1:𝖺𝖽𝖽(𝟢,y)yβ3:𝖿𝗌𝗍(x,y)xβ2:𝖺𝖽𝖽(𝗌(x),y)𝗌(𝖺𝖽𝖽(x,y)):subscript𝛽1absent𝖺𝖽𝖽0𝑦𝑦:subscript𝛽3absent𝖿𝗌𝗍𝑥𝑦𝑥:subscript𝛽2absent𝖺𝖽𝖽𝗌𝑥𝑦𝗌𝖺𝖽𝖽𝑥𝑦missing-subexpressionmissing-subexpressionmissing-subexpressionmissing-subexpression\begin{array}[]{lrcl@{~~~~~~~~~~}lrcl}\beta_{1}:&\mathsf{add}(\mathsf{0},y)&\to&y\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}~{}&\beta_{3}:&\mathsf{fst}(x,y)&\to&x\\ \beta_{2}:&\mathsf{add}(\mathsf{s}(x),y)&\to&\mathsf{s}(\mathsf{add}(x,y))\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}~{}\\ \end{array}

Given the term 𝖿𝗌𝗍(𝖺𝖽𝖽(𝗌(𝟢),𝟢),𝟢)𝖿𝗌𝗍𝖺𝖽𝖽𝗌000\mathsf{fst}(\mathsf{add}(\mathsf{s}(\mathsf{0}),\mathsf{0}),\mathsf{0}), we have, for instance, the following reversible (forward) derivation:

𝖿𝗌𝗍(𝖺𝖽𝖽(𝗌(𝟢),𝟢),𝟢),[]𝖿𝗌𝗍(𝗌(𝖺𝖽𝖽(𝟢,𝟢)),𝟢),[β2(1,id)]𝗌(𝖺𝖽𝖽(𝟢,𝟢)),[β3(ϵ,{y𝟢}),β2(1,id)]𝗌(𝟢),[β1(1,id),β3(ϵ,{y𝟢}),β2(1,id)]𝖿𝗌𝗍𝖺𝖽𝖽𝗌000subscript𝖿𝗌𝗍𝗌𝖺𝖽𝖽000delimited-[]subscript𝛽21𝑖𝑑missing-subexpressionsubscript𝗌𝖺𝖽𝖽00subscript𝛽3italic-ϵmaps-to𝑦0subscript𝛽21𝑖𝑑missing-subexpressionsubscript𝗌0subscript𝛽11𝑖𝑑subscript𝛽3italic-ϵmaps-to𝑦0subscript𝛽21𝑖𝑑\begin{array}[]{lll}\langle\mathsf{fst}(\mathsf{add}(\mathsf{s}(\mathsf{0}),\mathsf{0}),\mathsf{0}),{\color[rgb]{0,0,0}[\,]}\rangle&\rightharpoonup_{\mathcal{R}}&\langle\mathsf{fst}(\mathsf{s}(\mathsf{add}(\mathsf{0},\mathsf{0})),\mathsf{0}),{\color[rgb]{0,0,0}[\beta_{2}(1,id)]}\rangle\\ &\rightharpoonup_{\mathcal{R}}&\langle\mathsf{s}(\mathsf{add}(\mathsf{0},\mathsf{0})),{\color[rgb]{0,0,0}[\beta_{3}(\epsilon,\{y\mapsto\mathsf{0}\}),\beta_{2}(1,id)]}\rangle\\ &\rightharpoonup_{\mathcal{R}}&\langle\mathsf{s}(\mathsf{0}),{\color[rgb]{0,0,0}[\beta_{1}(1,id),\beta_{3}(\epsilon,\{y\mapsto\mathsf{0}\}),\beta_{2}(1,id)]}\rangle\\ \end{array}

The reader can easily check that 𝗌(𝟢),[β1(1,id),β3(ϵ,{y𝟢}),β2(1,id)]𝗌0subscript𝛽11𝑖𝑑subscript𝛽3italic-ϵmaps-to𝑦0subscript𝛽21𝑖𝑑\langle\mathsf{s}(\mathsf{0}),{\color[rgb]{0,0,0}[\beta_{1}(1,id),\beta_{3}(\epsilon,\{y\mapsto\mathsf{0}\}),\beta_{2}(1,id)]}\rangle is reducible to 𝖿𝗌𝗍(𝖺𝖽𝖽(𝗌(𝟢),𝟢),𝟢),[]𝖿𝗌𝗍𝖺𝖽𝖽𝗌000\langle\mathsf{fst}(\mathsf{add}(\mathsf{s}(\mathsf{0}),\mathsf{0}),\mathsf{0}),{\color[rgb]{0,0,0}[\,]}\rangle using the backward relation subscript\leftharpoondown_{\mathcal{R}} by performing exactly the same steps but in the backward direction.

An easy but essential property of subscript\rightharpoonup_{\mathcal{R}} is that it is a conservative extension of standard rewriting in the following sense (we omit its proof since it is straightforward):

Theorem 5

Let {\mathcal{R}} be a TRS. Given terms s,t𝑠𝑡s,t, if stsuperscriptsubscript𝑠𝑡s\to_{\mathcal{R}}^{\ast}t, then for any trace π𝜋\pi there exists a trace πsuperscript𝜋\pi^{\prime} such that s,πt,πsuperscriptsubscript𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}^{\ast}\langle t,\pi^{\prime}\rangle.

Here, and in the following, we assume that =()1subscriptsuperscriptsubscript1\mathrel{\leftarrow_{\mathcal{R}}}\,=\,\mathrel{(\to_{\mathcal{R}})^{-1}}, i.e., s1tsuperscriptsubscript1𝑠𝑡s\to_{\mathcal{R}}^{-1}t is denoted by stsubscript𝑠𝑡s\mathrel{\leftarrow_{\mathcal{R}}}t. Observe that the backward relation is not a conservative extension of subscript\mathrel{\leftarrow_{\mathcal{R}}}: in general, tssubscript𝑡𝑠t\leftarrow_{\mathcal{R}}s does not imply t,πs,πsubscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle for any arbitrary trace πsuperscript𝜋\pi^{\prime}. This is actually the purpose of our notion of reversible rewriting: subscript\leftharpoondown_{\mathcal{R}} should not extend subscript\leftarrow_{\mathcal{R}} but is only aimed at performing exactly the same steps of the forward computation whose trace was stored, but in the reverse order. Nevertheless, one can still ensure that for all steps tssubscript𝑡𝑠t\leftarrow_{\mathcal{R}}s, there exists some trace πsuperscript𝜋\pi^{\prime} such that t,πs,πsubscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle (which is an easy consequence of the above result and Theorem 9 below).

Example 6

Consider again the following TRS ={β:𝗌𝗇𝖽(x,y)y}conditional-set𝛽𝗌𝗇𝖽𝑥𝑦𝑦{\mathcal{R}}=\{\beta:~{}\mathsf{snd}(x,y)\to y\}. Given the reduction 𝗌𝗇𝖽(𝟣,𝟤)𝟤subscript𝗌𝗇𝖽122\mathsf{snd}(\mathsf{1},\mathsf{2})\to_{\mathcal{R}}\mathsf{2}, there are infinitely many reductions for 𝟤2\mathsf{\mathsf{2}} using subscript\leftarrow_{\mathcal{R}}, e.g., 𝟤𝗌𝗇𝖽(𝟣,𝟤)subscript2𝗌𝗇𝖽12\mathsf{2}\leftarrow_{\mathcal{R}}\mathsf{snd}(\mathsf{1},\mathsf{2}), 𝟤𝗌𝗇𝖽(𝟤,𝟤)subscript2𝗌𝗇𝖽22\mathsf{2}\leftarrow_{\mathcal{R}}\mathsf{snd}(\mathsf{2},\mathsf{2}), 𝟤𝗌𝗇𝖽(𝟥,𝟤)subscript2𝗌𝗇𝖽32\mathsf{2}\leftarrow_{\mathcal{R}}\mathsf{snd}(\mathsf{3},\mathsf{2}), etc. The relation is also non-terminating: 𝟤𝗌𝗇𝖽(𝟣,𝟤)𝗌𝗇𝖽(𝟣,𝗌𝗇𝖽(𝟣,𝟤))subscript2𝗌𝗇𝖽12subscript𝗌𝗇𝖽1𝗌𝗇𝖽12subscript\mathsf{2}\leftarrow_{\mathcal{R}}\mathsf{snd}(\mathsf{1},\mathsf{2})\leftarrow_{\mathcal{R}}\mathsf{snd}(\mathsf{1},\mathsf{snd}(\mathsf{1},\mathsf{2}))\leftarrow_{\mathcal{R}}\cdots. In contrast, given a pair 𝟤,π2𝜋\langle\mathsf{2},\pi\rangle, we can only perform a single deterministic and finite reduction (as proved below). For instance, if π=[β(ϵ,{x𝟣}),β(2,{x𝟣})]𝜋𝛽italic-ϵmaps-to𝑥1𝛽2maps-to𝑥1\pi=[\beta(\epsilon,\{x\mapsto\mathsf{\mathsf{1}}\}),\beta({2},\{x\mapsto\mathsf{\mathsf{1}}\})], then the only possible reduction is 𝟤,π𝗌𝗇𝖽(𝟣,𝟤),[β(2,{x𝟣})]𝗌𝗇𝖽(𝟣,𝗌𝗇𝖽(𝟣,𝟤)),[]↽̸subscript2𝜋𝗌𝗇𝖽12delimited-[]𝛽2maps-to𝑥1subscript𝗌𝗇𝖽1𝗌𝗇𝖽12subscript↽̸absent\langle\mathsf{\mathsf{2}},\pi\rangle\leftharpoondown_{\mathcal{R}}\langle\mathsf{snd}(\mathsf{\mathsf{1}},\mathsf{\mathsf{2}}),[\beta({2},\{x\mapsto\mathsf{\mathsf{1}}\})]\rangle\leftharpoondown_{\mathcal{R}}\langle\mathsf{snd}(\mathsf{\mathsf{1}},\mathsf{snd}(\mathsf{\mathsf{1}},\mathsf{\mathsf{2}})),[\,]\rangle\not\leftharpoondown_{\mathcal{R}}.

Now, we state a lemma which shows that safe pairs are preserved through reversible term rewriting (both in the forward and backward directions):

Lemma 7

Let {\mathcal{R}} be a TRS. Let s,π𝑠𝜋\langle s,\pi\rangle be a safe pair. If s,πt,π\langle s,\pi\rangle\rightleftharpoons^{\ast}_{\mathcal{R}}\langle t,\pi^{\prime}\rangle, then t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle is also safe.

Proof 1

We prove the claim by induction on the length k𝑘k of the derivation. Since the base case k=0𝑘0k=0 is trivial, consider the inductive case k>0𝑘0k>0. Assume a derivation of the form s,πs0,π0t,π\langle s,\pi\rangle\rightleftharpoons_{\mathcal{R}}^{\ast}\langle s_{0},\pi_{0}\rangle\rightleftharpoons_{\mathcal{R}}\langle t,\pi^{\prime}\rangle. By the induction hypothesis, we have that s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle is a safe pair. Now, we distinguish two cases depending on the last step. If we have s0,π0t,πsubscriptsubscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\rightharpoonup_{\mathcal{R}}\langle t,\pi^{\prime}\rangle, then there exist a position p𝒫𝗈𝗌(s0)𝑝𝒫𝗈𝗌subscript𝑠0p\in\mathsf{{{\cal P}}os}(s_{0}), a rewrite rule β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}}, and a ground substitution σ𝜎\sigma such that s0|p=lσevaluated-atsubscript𝑠0𝑝𝑙𝜎s_{0}|_{p}=l\sigma, t=s0[rσ]p𝑡subscript𝑠0subscriptdelimited-[]𝑟𝜎𝑝t=s_{0}[r\sigma]_{p}, σ=σ|`𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r)}, and π=β(p,σ):π0:superscript𝜋𝛽𝑝superscript𝜎subscript𝜋0\pi^{\prime}=\beta(p,\sigma^{\prime}):\pi_{0}. Then, since σsuperscript𝜎\sigma^{\prime} is ground and 𝒟𝗈𝗆(σ)=𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r) by construction, the claim follows straightforwardly. If the last step has the form s0,π0t,πsubscriptsubscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\leftharpoondown_{\mathcal{R}}\langle t,\pi^{\prime}\rangle, then the claim follows trivially since each step with subscript\leftharpoondown_{\mathcal{R}} only removes trace terms from π0subscript𝜋0\pi_{0}. \qed

Hence, since any pair with an empty trace is safe the following result, which states that every pair that is reachable from an initial pair with an empty trace is safe, straightforwardly follows from Lemma 7:

Proposition 8

Let {\mathcal{R}} be a TRS. If s,[]t,π\langle s,[\,]\rangle\rightleftharpoons_{\mathcal{R}}^{\ast}\langle t,\pi\rangle, then t,π𝑡𝜋\langle t,\pi\rangle is safe.

Now, we state the reversibility of subscript\rightharpoonup_{\mathcal{R}}, i.e., the fact that ()1=superscriptsubscript1subscript(\rightharpoonup_{\mathcal{R}})^{-1}=\>\leftharpoondown_{\mathcal{R}} (and thus the reversibility of subscript\leftharpoondown_{\mathcal{R}} and \rightleftharpoons_{\mathcal{R}}, too).

Theorem 9

Let {\mathcal{R}} be a TRS. Given the safe pairs s,π𝑠𝜋\langle s,\pi\rangle and t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle, for all n0𝑛0n\geq 0, s,πnt,πsuperscriptsubscript𝑛𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}^{n}\langle t,\pi^{\prime}\rangle iff t,πns,πsuperscriptsubscript𝑛𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}^{n}\langle s,\pi\rangle.

Proof 2

()(\Rightarrow) We prove the claim by induction on the length n𝑛n of the derivation s,πnt,πsuperscriptsubscript𝑛𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}^{n}\langle t,\pi^{\prime}\rangle. Since the base case n=0𝑛0n=0 is trivial, let us consider the inductive case n>0𝑛0n>0. Consider a derivation s,πn1s0,π0t,πsuperscriptsubscript𝑛1𝑠𝜋subscript𝑠0subscript𝜋0subscript𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}^{n-1}\langle s_{0},\pi_{0}\rangle\rightharpoonup_{\mathcal{R}}\langle t,\pi^{\prime}\rangle. By Lemma 7, both s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle and t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle are safe. By the induction hypothesis, we have s0,π0n1s,πsuperscriptsubscript𝑛1subscript𝑠0subscript𝜋0𝑠𝜋\langle s_{0},\pi_{0}\rangle\leftharpoondown_{\mathcal{R}}^{n-1}\langle s,\pi\rangle. Consider now the step s0,π0t,πsubscriptsubscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\rightharpoonup_{\mathcal{R}}\langle t,\pi^{\prime}\rangle. Then, there is a position p𝒫𝗈𝗌(s0)𝑝𝒫𝗈𝗌subscript𝑠0p\in\mathsf{{{\cal P}}os}(s_{0}), a rule β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}} and a ground substitution σ𝜎\sigma such that s0|p=lσevaluated-atsubscript𝑠0𝑝𝑙𝜎s_{0}|_{p}=l\sigma, t=s0[rσ]p𝑡subscript𝑠0subscriptdelimited-[]𝑟𝜎𝑝t=s_{0}[r\sigma]_{p}, σ=σ|`𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r)}, and π=β(p,σ):π0:superscript𝜋𝛽𝑝superscript𝜎subscript𝜋0\pi^{\prime}=\beta(p,\sigma^{\prime}):\pi_{0}. Let θ=σ|`𝒱𝖺𝗋(r)\theta=\sigma\!\!\mathrel{|\grave{}}_{\mathsf{{{\mathcal{V}}}ar}(r)}. Then, we have t,πs0,π0subscript𝑡superscript𝜋subscriptsuperscript𝑠0subscript𝜋0\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s^{\prime}_{0},\pi_{0}\rangle with t|p=rθevaluated-at𝑡𝑝𝑟𝜃t|_{p}=r\theta, β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}} and s0=t[lθσ]psubscriptsuperscript𝑠0𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s^{\prime}_{0}=t[l\theta\sigma^{\prime}]_{p}. Moreover, since σ=θσ𝜎𝜃superscript𝜎\sigma=\theta\sigma^{\prime}, we have s0=t[lθσ]p=t[lσ]p=s0subscriptsuperscript𝑠0𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝𝑡subscriptdelimited-[]𝑙𝜎𝑝subscript𝑠0s^{\prime}_{0}=t[l\theta\sigma^{\prime}]_{p}=t[l\sigma]_{p}=s_{0}, and the claim follows.

()(\Leftarrow) This direction proceeds in a similar way. We prove the claim by induction on the length n𝑛n of the derivation t,πns,πsuperscriptsubscript𝑛𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}^{n}\langle s,\pi\rangle. As before, we only consider the inductive case n>0𝑛0n>0. Let us consider a derivation t,πn1s0,π0s,πsuperscriptsubscript𝑛1𝑡superscript𝜋subscript𝑠0subscript𝜋0subscript𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}^{n-1}\langle s_{0},\pi_{0}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle. By Lemma 7, both s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle and s,π𝑠𝜋\langle s,\pi\rangle are safe. By the induction hypothesis, we have s0,π0n1t,πsuperscriptsubscript𝑛1subscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\rightharpoonup_{\mathcal{R}}^{n-1}\langle t,\pi^{\prime}\rangle. Consider now the reduction step s0,π0s,πsubscriptsubscript𝑠0subscript𝜋0𝑠𝜋\langle s_{0},\pi_{0}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle. Then, we have π0=β(p,σ):π:subscript𝜋0𝛽𝑝superscript𝜎𝜋\pi_{0}=\beta(p,\sigma^{\prime}):\pi, β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}}, and there exists a ground substitution θ𝜃\theta with 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜃𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r) such that s0|p=rθevaluated-atsubscript𝑠0𝑝𝑟𝜃s_{0}|_{p}=r\theta and s=s0[lθσ]p𝑠subscript𝑠0subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s=s_{0}[l\theta\sigma^{\prime}]_{p}. Moreover, since s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle is safe, we have that 𝒟𝗈𝗆(σ)=𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r) and, thus, 𝒟𝗈𝗆(θ)𝒟𝗈𝗆(σ)=𝒟𝗈𝗆𝜃𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)\cap\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\varnothing. Let σ=θσ𝜎𝜃superscript𝜎\sigma=\theta\sigma^{\prime}. Then, since s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma and 𝒟𝗈𝗆(σ)=𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r), we can perform the step s,πs0,β(p,σ):π\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}\langle s^{\prime}_{0},\beta(p,\sigma^{\prime}):\pi\rangle with s0=s[rσ]p=s[rθσ]p=s[rθ]p=s0[rθ]p=s0subscriptsuperscript𝑠0𝑠subscriptdelimited-[]𝑟𝜎𝑝𝑠subscriptdelimited-[]𝑟𝜃superscript𝜎𝑝𝑠subscriptdelimited-[]𝑟𝜃𝑝subscript𝑠0subscriptdelimited-[]𝑟𝜃𝑝subscript𝑠0s^{\prime}_{0}=s[r\sigma]_{p}=s[r\theta\sigma^{\prime}]_{p}=s[r\theta]_{p}=s_{0}[r\theta]_{p}=s_{0}, and the claim follows. \qed

The next corollary is then immediate:

Corollary 10

Let {\mathcal{R}} be a TRS. Given the safe pairs s,π𝑠𝜋\langle s,\pi\rangle and t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle, for all n0𝑛0n\geq 0, s,πnt,π\langle s,\pi\rangle\rightleftharpoons_{\mathcal{R}}^{n}\langle t,\pi^{\prime}\rangle iff t,πns,π\langle t,\pi^{\prime}\rangle\rightleftharpoons_{\mathcal{R}}^{n}\langle s,\pi\rangle.

A key issue of our notion of reversible rewriting is that the backward rewrite relation subscript\leftharpoondown_{\mathcal{R}} is deterministic (thus confluent), terminating, and has a constructive definition:

Theorem 11

Let {\mathcal{R}} be a TRS. Given a safe pair t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle, there exists at most one pair s,π𝑠𝜋\langle s,\pi\rangle such that t,πs,πsubscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle.

Proof 3

First, if there is no step using subscript\leftharpoondown_{\mathcal{R}} from t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle, the claim follows trivially. Now, assume there is at least one step t,πs,πsubscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle. We prove that this is the only possible step. By definition, we have π=β(p,σ):π:superscript𝜋𝛽𝑝superscript𝜎𝜋\pi^{\prime}=\beta(p,\sigma^{\prime}):\pi, p𝒫𝗈𝗌(t)𝑝𝒫𝗈𝗌𝑡p\in\mathsf{{{\cal P}}os}(t), β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}}, and there exists a ground substitution θ𝜃\theta with 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜃𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r) such that t|p=rθevaluated-at𝑡𝑝𝑟𝜃t|_{p}=r\theta and s=t[lθσ]p𝑠𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s=t[l\theta\sigma^{\prime}]_{p}. The only source of nondeterminism may come from choosing a rule labeled with β𝛽\beta and from the computation of the substitution θ𝜃\theta. The claim follows trivially from the fact that labels are unique in {\mathcal{R}} and that, if there is some ground substitution θsuperscript𝜃\theta^{\prime} with θ=𝒱𝖺𝗋(r)superscript𝜃𝒱𝖺𝗋𝑟\theta^{\prime}=\mathsf{{{\mathcal{V}}}ar}(r) and t|p=rθevaluated-at𝑡𝑝𝑟superscript𝜃t|_{p}=r\theta^{\prime}, then θ=θ𝜃superscript𝜃\theta=\theta^{\prime}. \qed

Therefore, subscript\leftharpoondown_{\mathcal{R}} is clearly deterministic and confluent. Termination holds straightforwardly for pairs with finite traces since its length strictly decreases with every backward step. Note however that even when subscript\rightharpoonup_{\mathcal{R}} and subscript\leftharpoondown_{\mathcal{R}} are terminating, the relation \rightleftharpoons_{\mathcal{R}} is always non-terminating since one can keep going back and forth.

3.2 Conditional Term Rewrite Systems

In this section, we extend the previous notions and results to DCTRSs. We note that considering DCTRSs is not enough to make conditional rewriting deterministic. In general, given a rewrite step sp,βtsubscript𝑝𝛽𝑠𝑡s\to_{p,\beta}t with p𝑝p a position of s𝑠s, β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\to t_{n}} a rule, and σ𝜎\sigma a substitution such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma and siσtiσsubscriptsuperscriptsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎s_{i}\sigma\to^{\ast}_{\mathcal{R}}t_{i}\sigma for all i=1,,n𝑖1𝑛i=1,\ldots,n, there are three potential sources of non-determinism: the selected position p𝑝p, the selected rule β𝛽\beta, and the substitution σ𝜎\sigma. The use of DCTRSs can only make deterministic the last one, but the choice of a position and the selection of a rule may still be non-deterministic.

For DCTRSs, the notion of a trace term used for TRSs is not sufficient since we also need to store the traces of the subderivations associated to the condition of the applied rule (if any). Therefore, we generalize the notion of a trace as follows:

Definition 12 (trace)

Given a CTRS {\mathcal{R}}, a trace in {\mathcal{R}} is recursively defined as follows:

  • 1.

    the empty list is a trace;

  • 2.

    if π,π1,,πn𝜋subscript𝜋1subscript𝜋𝑛\pi,\pi_{1},\ldots,\pi_{n} are traces in {\mathcal{R}}, n0𝑛0n\geq 0, β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}} is a rule, p𝑝p is a position, and σ𝜎\sigma is a ground substitution, then β(p,σ,π1,,πn):π:𝛽𝑝𝜎subscript𝜋1subscript𝜋𝑛𝜋\beta(p,\sigma,\pi_{1},\ldots,\pi_{n}):\pi is a trace in {\mathcal{R}}.

We refer to each component β(p,σ,π1,,πn)𝛽𝑝𝜎subscript𝜋1subscript𝜋𝑛\beta(p,\sigma,\pi_{1},\ldots,\pi_{n}) in a trace as a trace term.

Intuitively speaking, a trace term β(p,σ,π1,,πn)𝛽𝑝𝜎subscript𝜋1subscript𝜋𝑛\beta(p,\sigma,\pi_{1},\ldots,\pi_{n}) stores the position of a reduction step, a substitution with the bindings that are required for the step to be reversible (e.g., the bindings for the erased variables, but not only; see below) and the traces associated to the subcomputations in the condition.

The notion of a safe pair is now more involved in order to deal with conditional rules. The motivation for this definition will be explained below, after introducing reversible rewriting for DCTRSs.

Definition 13 (safe pair)

Let {\mathcal{R}} be a DCTRS. A trace π𝜋\pi is safe in {\mathcal{R}} iff, for all trace terms β(p,σ,πn¯)𝛽𝑝𝜎¯subscript𝜋𝑛\beta(p,\sigma,\overline{\pi_{n}}) in π𝜋\pi, σ𝜎\sigma is a ground substitution with 𝒟𝗈𝗆(σ)=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)𝒟𝗈𝗆𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\mathsf{{{\mathcal{D}}}om}(\sigma)=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}), β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and πn¯¯subscript𝜋𝑛\overline{\pi_{n}} are safe too. The pair s,π𝑠𝜋\langle s,\pi\rangle is safe in {\mathcal{R}} iff π𝜋\pi is safe.

Reversible (conditional) rewriting can now be introduced as follows:

Definition 14 (reversible rewriting)

Let {\mathcal{R}} be a DCTRS. The reversible rewrite relation subscript\rightharpoonup_{\mathcal{R}} is defined on safe pairs t,π𝑡𝜋\langle t,\pi\rangle, where t𝑡t is a ground term and π𝜋\pi is a trace in {\mathcal{R}}. The reversible rewrite relation extends standard conditional rewriting as follows:

s,πt,β(p,σ,π1,,πn):π\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}\langle t,\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}):\pi\rangle

iff there exist a position p𝒫𝗈𝗌(s)𝑝𝒫𝗈𝗌𝑠p\in\mathsf{{{\cal P}}os}(s), a rewrite rule β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a ground substitution σ𝜎\sigma such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma, siσ,[]tiσ,πisuperscriptsubscriptsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎subscript𝜋𝑖\langle s_{i}\sigma,[\,]\rangle\rightharpoonup_{\mathcal{R}}^{\ast}\langle t_{i}\sigma,\pi_{i}\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n, t=s[rσ]p𝑡𝑠subscriptdelimited-[]𝑟𝜎𝑝t=s[r\sigma]_{p}, and σ=σ|`(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}})}. The reverse relation, subscript\leftharpoondown_{\mathcal{R}}, is then defined as follows:

t,β(p,σ,π1,,πn):πs,π\langle t,\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}):\pi\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle

iff t,β(p,σ,πn¯):πdelimited-⟨⟩:𝑡𝛽𝑝superscript𝜎¯subscript𝜋𝑛𝜋\langle t,\beta(p,\sigma^{\prime},\overline{\pi_{n}}):\pi\rangle is a safe pair in {\mathcal{R}}, β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}} and there is a ground substitution θ𝜃\theta such that 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r,sn¯)\𝒟𝗈𝗆(σ)𝒟𝗈𝗆𝜃\𝒱𝖺𝗋𝑟¯subscript𝑠𝑛𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}})\backslash\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime}), t|p=rθevaluated-at𝑡𝑝𝑟𝜃t|_{p}=r\theta, tiθσ,πisiθσ,[]superscriptsubscriptsubscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖subscript𝑠𝑖𝜃superscript𝜎\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle\leftharpoondown_{\mathcal{R}}^{\ast}\langle s_{i}\theta\sigma^{\prime},[\,]\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n, and s=t[lθσ]p𝑠𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s=t[l\theta\sigma^{\prime}]_{p}. Note that θσ=σθ=θσ𝜃superscript𝜎superscript𝜎𝜃𝜃superscript𝜎\theta\sigma^{\prime}=\sigma^{\prime}\theta=\theta\cup\sigma^{\prime} since 𝒟𝗈𝗆(θ)𝒟𝗈𝗆(σ)=𝒟𝗈𝗆𝜃𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)\cap\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\varnothing and both substitutions are ground.

As in the unconditional case, we denote the union of both relations subscriptsubscript\rightharpoonup_{\mathcal{R}}\cup\leftharpoondown_{\mathcal{R}} by \rightleftharpoons_{\mathcal{R}}.

Example 15

Consider again the DCTRS {\mathcal{R}} from Example 1:

β1:𝖺𝖽𝖽(𝟢,y)yβ4:𝖾𝗏𝖾𝗇(𝟢)𝗍𝗋𝗎𝖾β2:𝖺𝖽𝖽(𝗌(x),y)𝗌(𝖺𝖽𝖽(x,y))β5:𝖾𝗏𝖾𝗇(𝗌(𝗌(x)))𝖾𝗏𝖾𝗇(x)β3:𝖽𝗈𝗎𝖻𝗅𝖾(x)𝖺𝖽𝖽(x,x)𝖾𝗏𝖾𝗇(x)𝗍𝗋𝗎𝖾:subscript𝛽1absent𝖺𝖽𝖽0𝑦𝑦:subscript𝛽4absent𝖾𝗏𝖾𝗇0𝗍𝗋𝗎𝖾:subscript𝛽2absent𝖺𝖽𝖽𝗌𝑥𝑦𝗌𝖺𝖽𝖽𝑥𝑦:subscript𝛽5absent𝖾𝗏𝖾𝗇𝗌𝗌𝑥𝖾𝗏𝖾𝗇𝑥:subscript𝛽3absent𝖽𝗈𝗎𝖻𝗅𝖾𝑥𝖺𝖽𝖽𝑥𝑥𝖾𝗏𝖾𝗇𝑥𝗍𝗋𝗎𝖾missing-subexpressionmissing-subexpressionmissing-subexpressionmissing-subexpression\begin{array}[]{l@{~}r@{~}c@{~}l@{}l@{~}r@{~}c@{~}l}\beta_{1}:\hfil~{}&\mathsf{add}(\mathsf{0},y)~{}&\to\hfil~{}&y&\beta_{4}:\hfil~{}&\mathsf{even}(\mathsf{0})~{}&\to\hfil~{}&\mathsf{true}\\ \beta_{2}:\hfil~{}&\mathsf{add}(\mathsf{s}(x),y)~{}&\to\hfil~{}&\mathsf{s}(\mathsf{add}(x,y))&\beta_{5}:\hfil~{}&\mathsf{even}(\mathsf{s}(\mathsf{s}(x)))~{}&\to\hfil~{}&\mathsf{even}(x)\\ \beta_{3}:\hfil~{}&\mathsf{double}(x)~{}&\to\hfil~{}&\mathsf{add}(x,x)\Leftarrow\mathsf{even}(x)\twoheadrightarrow\mathsf{true}\\ \end{array}

Given the term 𝖽𝗈𝗎𝖻𝗅𝖾(𝗌(𝗌(𝟢)))𝖽𝗈𝗎𝖻𝗅𝖾𝗌𝗌0\mathsf{double}(\mathsf{s}(\mathsf{s}(\mathsf{0}))), we have, for instance, the following forward derivation:

𝖽𝗈𝗎𝖻𝗅𝖾(𝗌(𝗌(𝟢))),[]𝖺𝖽𝖽(𝗌(𝗌(𝟢)),𝗌(𝗌(𝟢))),[β3(ϵ,id,π)]𝗌(𝗌(𝗌(𝗌(𝟢)))),[β1(1.1,id),β2(1,id),β2(ϵ,id),β3(ϵ,id,π)]\begin{array}[]{lll}\langle\mathsf{double}(\mathsf{s}(\mathsf{s}(\mathsf{0}))),{[\,]}\rangle\\ \mbox{}\hskip 43.05542pt\rightharpoonup_{\mathcal{R}}~{}\langle\mathsf{add}(\mathsf{s}(\mathsf{s}(\mathsf{0})),\mathsf{s}(\mathsf{s}(\mathsf{0}))),{[\beta_{3}(\epsilon,id,{\pi})]}\rangle\\ \mbox{}\hskip 43.05542pt\rightharpoonup_{\mathcal{R}}~{}\cdots\\ \mbox{}\hskip 43.05542pt\rightharpoonup_{\mathcal{R}}~{}\langle\mathsf{s}(\mathsf{s}(\mathsf{s}(\mathsf{s}(\mathsf{0})))),{[\beta_{1}(1.1,id),\beta_{2}(1,id),\beta_{2}(\epsilon,id),\beta_{3}(\epsilon,id,{\pi})]}\rangle\\ \end{array}

where π=[β4(ϵ,id),β5(ϵ,id)]𝜋subscript𝛽4italic-ϵ𝑖𝑑subscript𝛽5italic-ϵ𝑖𝑑{\pi}={[\beta_{4}(\epsilon,id),\beta_{5}(\epsilon,id)]} since we have the following reduction:

𝖾𝗏𝖾𝗇(𝗌(𝗌(𝟢))),[]𝖾𝗏𝖾𝗇(𝟢),[β5(ϵ,id)]𝗍𝗋𝗎𝖾,[β4(ϵ,id),β5(ϵ,id)]subscript𝖾𝗏𝖾𝗇𝗌𝗌0𝖾𝗏𝖾𝗇0delimited-[]subscript𝛽5italic-ϵ𝑖𝑑subscript𝗍𝗋𝗎𝖾subscript𝛽4italic-ϵ𝑖𝑑subscript𝛽5italic-ϵ𝑖𝑑\langle\mathsf{even}(\mathsf{s}(\mathsf{s}(\mathsf{0}))),{[\,]}\rangle\rightharpoonup_{\mathcal{R}}\langle\mathsf{even}(\mathsf{0}),{[\beta_{5}(\epsilon,id)]}\rangle\rightharpoonup_{\mathcal{R}}\langle\mathsf{true},{[\beta_{4}(\epsilon,id),\beta_{5}(\epsilon,id)]}\rangle

The reader can easily construct the associated backward derivation:

𝖺𝖽𝖽(𝗌(𝗌(𝟢)),𝗌(𝗌(𝟢))),[β1(1.1,id),β2(1,id),]𝖽𝗈𝗎𝖻𝗅𝖾(𝗌(𝗌(𝟢))),[]superscriptsubscript𝖺𝖽𝖽𝗌𝗌0𝗌𝗌0subscript𝛽11.1𝑖𝑑subscript𝛽21𝑖𝑑𝖽𝗈𝗎𝖻𝗅𝖾𝗌𝗌0\langle\mathsf{add}(\mathsf{s}(\mathsf{s}(\mathsf{0})),\mathsf{s}(\mathsf{s}(\mathsf{0}))),{[\beta_{1}(1.1,id),\beta_{2}(1,id),\ldots]}\rangle\leftharpoondown_{\mathcal{R}}^{\ast}\langle\mathsf{double}(\mathsf{s}(\mathsf{s}(\mathsf{0}))),{[\,]}\rangle

Let us now explain why we need to store σsuperscript𝜎\sigma^{\prime} in a step of the form s,πt,β(p,σ,πn¯):π\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}\langle t,\beta(p,\sigma^{\prime},\overline{\pi_{n}}):\pi\rangle. Given a DCTRS, for each rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}, the following conditions hold:

  • 1.

    3-CTRS: 𝒱𝖺𝗋(r)𝒱𝖺𝗋(l,sn¯,tn¯)𝒱𝖺𝗋𝑟𝒱𝖺𝗋𝑙¯subscript𝑠𝑛¯subscript𝑡𝑛\mathsf{{{\mathcal{V}}}ar}(r)\subseteq\mathsf{{{\mathcal{V}}}ar}(l,\overline{s_{n}},\overline{t_{n}}).

  • 2.

    Determinism: for all i=1,,n𝑖1𝑛i=1,\ldots,n, we have 𝒱𝖺𝗋(si)𝒱𝖺𝗋(l,ti1¯)𝒱𝖺𝗋subscript𝑠𝑖𝒱𝖺𝗋𝑙¯subscript𝑡𝑖1\mathsf{{{\mathcal{V}}}ar}(s_{i})\subseteq\mathsf{{{\mathcal{V}}}ar}(l,\overline{t_{i-1}}).

Intuitively, the backward relation subscript\leftharpoondown_{\mathcal{R}} can be seen as equivalent to the forward relation subscript\rightharpoonup_{\mathcal{R}} but using a reverse rule of the form rltnsn,,t1s1formulae-sequence𝑟𝑙subscript𝑡𝑛subscript𝑠𝑛subscript𝑡1subscript𝑠1r\to l\Leftarrow t_{n}\twoheadrightarrow s_{n},\ldots,t_{1}\twoheadrightarrow s_{1}. Therefore, in order to ensure that backward reduction is deterministic, we need the same conditions as above but on the reverse rule:555We note that the notion of a non-erasing rule is extended to the DCTRSs in [32], which results in a similar condition.

  • 1.

    3-CTRS: 𝒱𝖺𝗋(l)𝒱𝖺𝗋(r,sn¯,tn¯)𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛\mathsf{{{\mathcal{V}}}ar}(l)\subseteq\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}).

  • 2.

    Determinism: for all i=1,,n𝑖1𝑛i=1,\ldots,n, 𝒱𝖺𝗋(ti)𝒱𝖺𝗋(r,si+1,n¯)𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\mathsf{{{\mathcal{V}}}ar}(t_{i})\subseteq\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}).

Since these conditions cannot be guaranteed in general, we store

σ=σ|`(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}})}

in the trace term so that (rltnsn,,t1s1)σ(r\to l\Leftarrow t_{n}\twoheadrightarrow s_{n},\ldots,t_{1}\twoheadrightarrow s_{1})\sigma^{\prime} is deterministic and fulfills the conditions of a 3-CTRS by construction, i.e., 𝒱𝖺𝗋(lσ)𝒱𝖺𝗋(rσ,snσ¯,tnσ¯)𝒱𝖺𝗋𝑙superscript𝜎𝒱𝖺𝗋𝑟superscript𝜎¯subscript𝑠𝑛superscript𝜎¯subscript𝑡𝑛superscript𝜎\mathsf{{{\mathcal{V}}}ar}(l\sigma^{\prime})\subseteq\mathsf{{{\mathcal{V}}}ar}(r\sigma^{\prime},\overline{s_{n}\sigma^{\prime}},\overline{t_{n}\sigma^{\prime}}) and for all i=1,,n𝑖1𝑛i=1,\ldots,n, 𝒱𝖺𝗋(tiσ)𝒱𝖺𝗋(rσ,si+1,nσ¯)𝒱𝖺𝗋subscript𝑡𝑖superscript𝜎𝒱𝖺𝗋𝑟superscript𝜎¯subscript𝑠𝑖1𝑛superscript𝜎\mathsf{{{\mathcal{V}}}ar}(t_{i}\sigma^{\prime})\subseteq\mathsf{{{\mathcal{V}}}ar}(r\sigma^{\prime},\overline{s_{i+1,n}\sigma^{\prime}}); see the proof of Theorem 21 for more details.

Example 16

Consider the following DCTRS:

β1:𝖿(x,y,m)𝗌(w)𝗁(x)x,𝗀(y,𝟦)wβ2:𝗁(𝟢)𝟢β3:𝗁(𝟣)𝟣β4:𝗀(x,y)x\begin{array}[]{l}\beta_{1}:~{}\mathsf{f}(x,y,m)~{}\to~{}\mathsf{s}(w)\Leftarrow\mathsf{h}(x)\twoheadrightarrow x,\mathsf{g}(y,\mathsf{4})\twoheadrightarrow w\\ \beta_{2}:~{}\mathsf{h}(\mathsf{0})~{}\to~{}\mathsf{0}~{}~{}~{}~{}~{}~{}~{}~{}~{}\beta_{3}:~{}\mathsf{h}(\mathsf{1})~{}\to~{}\mathsf{1}~{}~{}~{}~{}~{}~{}~{}~{}~{}\beta_{4}:~{}\mathsf{g}(x,y)~{}\to~{}x\\ \end{array}

and the step 𝖿(𝟢,𝟤,𝟦),[]𝗌(𝟤),[β1(ϵ,σ,π1,π2)]subscript𝖿024𝗌2delimited-[]subscript𝛽1italic-ϵsuperscript𝜎subscript𝜋1subscript𝜋2\langle\mathsf{f}(\mathsf{0},\mathsf{2},\mathsf{4}),[\,]\rangle\rightharpoonup_{\mathcal{R}}\langle\mathsf{s}(\mathsf{2}),[\beta_{1}(\epsilon,\sigma^{\prime},\pi_{1},\pi_{2})]\rangle with σ={m𝟦,x𝟢}superscript𝜎formulae-sequencemaps-to𝑚4maps-to𝑥0\sigma^{\prime}=\{m\mapsto\mathsf{4},x\mapsto\mathsf{0}\}, π1=[β2(ϵ,id)]subscript𝜋1delimited-[]subscript𝛽2italic-ϵ𝑖𝑑\pi_{1}=[\beta_{2}(\epsilon,id)] and π2=[β4(ϵ,{y𝟦})]subscript𝜋2delimited-[]subscript𝛽4italic-ϵmaps-to𝑦4\pi_{2}=[\beta_{4}(\epsilon,\{y\mapsto\mathsf{4}\})]. The binding of variable m𝑚m is required to recover the value of the erased variable m𝑚m, but the binding of variable x𝑥x is also needed to perform the subderivation x,π1𝗁(x),[]subscript𝑥subscript𝜋1𝗁𝑥\langle x,\pi_{1}\rangle\leftharpoondown_{\mathcal{R}}\langle\mathsf{h}(x),[\,]\rangle when applying a backward step from 𝗌(𝟤),[β1(ϵ,σ,π1,π2)]𝗌2delimited-[]subscript𝛽1italic-ϵsuperscript𝜎subscript𝜋1subscript𝜋2\langle\mathsf{s}(\mathsf{2}),[\beta_{1}(\epsilon,\sigma^{\prime},\pi_{1},\pi_{2})]\rangle. If the binding for x𝑥x were unknown, this step would not be deterministic. As mentioned above, an instantiated reverse rule (𝗌(w)𝖿(x,y,m)w𝗀(y,𝟦),x𝗁(x))σ=𝗌(w)𝖿(𝟢,y,𝟦)w𝗀(y,𝟦),𝟢𝗁(𝟢)(\mathsf{s}(w)\to\mathsf{f}(x,y,m)\Leftarrow w\twoheadrightarrow\mathsf{g}(y,\mathsf{4}),x\twoheadrightarrow\mathsf{h}(x))\sigma^{\prime}~{}=~{}\mathsf{s}(w)\to\mathsf{f}(\mathsf{0},y,\mathsf{4})\Leftarrow w\twoheadrightarrow\mathsf{g}(y,\mathsf{4}),\mathsf{0}\twoheadrightarrow\mathsf{h}(\mathsf{0}) would be a legal DCTRS rule thanks to σsuperscript𝜎\sigma^{\prime}.

We note that similar conditions could be defined for arbitrary 3-CTRSs. However, the conditions would be much more involved; e.g., one had to compute first the variable dependencies between the equations in the conditions. Therefore, we prefer to keep the simpler conditions for DCTRSs (where these dependencies are fixed), which is still quite a general class of CTRSs.

Reversible rewriting is also a conservative extension of rewriting for DCTRSs (we omit the proof since it is straightforward):

Theorem 17

Let {\mathcal{R}} be a DCTRS. Given ground terms s,t𝑠𝑡s,t, if stsuperscriptsubscript𝑠𝑡s\to_{\mathcal{R}}^{\ast}t, then for any trace π𝜋\pi there exists a trace πsuperscript𝜋\pi^{\prime} such that s,πt,πsuperscriptsubscript𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{\mathcal{R}}^{\ast}\langle t,\pi^{\prime}\rangle.

For the following result, we need some preliminary notions (see, e.g., [36]). For every oriented CTRS {\mathcal{R}}, we inductively define the TRSs ksubscript𝑘{\mathcal{R}}_{k}, k0𝑘0k\geq 0, as follows:

0=k+1={lσrσlrsntn¯,siσktiσfor alli=1,,n}subscript0subscript𝑘1conditional-set𝑙𝜎𝑟𝜎formulae-sequence𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛subscriptsuperscriptsubscript𝑘subscript𝑠𝑖𝜎subscript𝑡𝑖𝜎for all𝑖1𝑛\begin{array}[]{l@{~}l@{~}l}{\mathcal{R}}_{0}\hfil~{}&=\hfil~{}&\varnothing\\ {\mathcal{R}}_{k+1}\hfil~{}&=\hfil~{}&\{l\sigma\to r\sigma\mid l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}},~{}s_{i}\sigma\to^{\ast}_{{\mathcal{R}}_{k}}t_{i}\sigma~{}\mbox{for all}~{}i=1,\ldots,n\}\end{array}

Observe that kk+1subscript𝑘subscript𝑘1{\mathcal{R}}_{k}\subseteq{\mathcal{R}}_{k+1} for all k0𝑘0k\geq 0. We have =i0isubscriptsubscript𝑖0subscriptsubscript𝑖\to_{\mathcal{R}}=\bigcup_{i\geq 0}\to_{{\mathcal{R}}_{i}}. We also have stsubscript𝑠𝑡s\to_{\mathcal{R}}t iff sktsubscriptsubscript𝑘𝑠𝑡s\to_{{\mathcal{R}}_{k}}t for some k0𝑘0k\geq 0. The minimum such k𝑘k is called the depth of stsubscript𝑠𝑡s\to_{\mathcal{R}}t, and the maximum depth k𝑘k of s=s0k1kmsm=t𝑠subscript𝑠0subscriptsubscriptsubscript𝑘1subscriptsubscriptsubscript𝑘𝑚subscript𝑠𝑚𝑡s=s_{0}\to_{{\mathcal{R}}_{k_{1}}}\cdots\to_{{\mathcal{R}}_{k_{m}}}s_{m}=t (i.e., k𝑘k is the maximum of depths k1,,kmsubscript𝑘1subscript𝑘𝑚k_{1},\ldots,k_{m}) is called the depth of the derivation. If a derivation has depth k𝑘k and length m𝑚m, we write skmtsuperscriptsubscriptsubscript𝑘𝑚𝑠𝑡s\to_{{\mathcal{R}}_{k}}^{m}t. Analogous notions can naturally be defined for subscript\rightharpoonup_{\mathcal{R}}, subscript\leftharpoondown_{\mathcal{R}}, and \rightleftharpoons_{\mathcal{R}}.

The next result shows that safe pairs are also preserved through reversible rewriting with DCTRSs:

Lemma 18

Let {\mathcal{R}} be a DCTRS and s,π𝑠𝜋\langle s,\pi\rangle a safe pair. If s,πt,π\langle s,\pi\rangle\rightleftharpoons_{\mathcal{R}}^{\ast}\langle t,\pi^{\prime}\rangle, then t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle is also safe.

Proof 4

We prove the claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the derivation s,πkmt,π\langle s,\pi\rangle\rightleftharpoons_{{\mathcal{R}}_{k}}^{m}\langle t,\pi^{\prime}\rangle. Since the base case is trivial, we consider the inductive case (k,m)>(0,0)𝑘𝑚00(k,m)>(0,0). Consider a derivation s,πkm1s0,π0kt,π\langle s,\pi\rangle\rightleftharpoons_{{\mathcal{R}}_{k}}^{m-1}\langle s_{0},\pi_{0}\rangle\rightleftharpoons_{{\mathcal{R}}_{k}}\langle t,\pi^{\prime}\rangle. By the induction hypothesis, we have that s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle is safe. Now, we distinguish two cases depending on the last step. If the last step is s0,π0kt,πsubscriptsubscript𝑘subscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\rightharpoonup_{{\mathcal{R}}_{k}}\langle t,\pi^{\prime}\rangle, then there exist a position p𝒫𝗈𝗌(s0)𝑝𝒫𝗈𝗌subscript𝑠0p\in\mathsf{{{\cal P}}os}(s_{0}), a rewrite rule β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a ground substitution σ𝜎\sigma such that s0|p=lσevaluated-atsubscript𝑠0𝑝𝑙𝜎s_{0}|_{p}=l\sigma, siσ,[]kitiσ,πisuperscriptsubscriptsubscriptsubscript𝑘𝑖subscript𝑠𝑖𝜎subscript𝑡𝑖𝜎subscript𝜋𝑖\langle s_{i}\sigma,[\,]\rangle\rightharpoonup_{{\mathcal{R}}_{k_{i}}}^{\ast}\langle t_{i}\sigma,\pi_{i}\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n, t=s0[rσ]p𝑡subscript𝑠0subscriptdelimited-[]𝑟𝜎𝑝t=s_{0}[r\sigma]_{p}, σ=σ|`(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}})}, and π=β(p,σ,π1,,πn)superscript𝜋𝛽𝑝superscript𝜎subscript𝜋1subscript𝜋𝑛\pi^{\prime}=\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}). Then, since ki<ksubscript𝑘𝑖𝑘k_{i}<k, i=1,,n𝑖1𝑛i=1,\ldots,n, σsuperscript𝜎\sigma^{\prime} is ground and 𝒟𝗈𝗆(σ)=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}) by construction, the claim follows by induction. Finally, if the last step has the form s0,π0kt,πsubscriptsubscript𝑘subscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}\langle t,\pi^{\prime}\rangle, then the claim follows trivially since a step with subscript\leftharpoondown_{\mathcal{R}} only removes trace terms from π0subscript𝜋0\pi_{0}. \qed

As in the unconditional case, the following proposition follows straightforwardly from the previous lemma since any pair with an empty trace is safe.

Proposition 19

Let {\mathcal{R}} be a DCTRS. If s,[]t,π\langle s,[\,]\rangle\rightleftharpoons_{\mathcal{R}}^{\ast}\langle t,\pi\rangle, then t,π𝑡𝜋\langle t,\pi\rangle is safe in {\mathcal{R}}.

Now, we can already state the reversibility of subscript\rightharpoonup_{\mathcal{R}} for DCTRSs:

Theorem 20

Let {\mathcal{R}} be a DCTRS. Given the safe pairs s,π𝑠𝜋\langle s,\pi\rangle and t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle, for all k,m0𝑘𝑚0k,m\geq 0, s,πkmt,πsuperscriptsubscriptsubscript𝑘𝑚𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{{\mathcal{R}}_{k}}^{m}\langle t,\pi^{\prime}\rangle iff t,πkms,πsuperscriptsubscriptsubscript𝑘𝑚𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}^{m}\langle s,\pi\rangle.

Proof 5

()(\Rightarrow) We prove the claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the derivation s,πkmt,πsuperscriptsubscriptsubscript𝑘𝑚𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{{\mathcal{R}}_{k}}^{m}\langle t,\pi^{\prime}\rangle. Since the base case is trivial, we consider the inductive case (k,m)>(0,0)𝑘𝑚00(k,m)>(0,0). Consider a derivation s,πkm1s0,π0kt,πsuperscriptsubscriptsubscript𝑘𝑚1𝑠𝜋subscript𝑠0subscript𝜋0subscriptsubscript𝑘𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{{\mathcal{R}}_{k}}^{m-1}\langle s_{0},\pi_{0}\rangle\rightharpoonup_{{\mathcal{R}}_{k}}\langle t,\pi^{\prime}\rangle whose associated product is (k,m)𝑘𝑚(k,m). By Proposition 19, both s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle and t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle are safe. By the induction hypothesis, since (k,m1)<(k,m)𝑘𝑚1𝑘𝑚(k,m-1)<(k,m), we have s0,π0km1s,πsuperscriptsubscriptsubscript𝑘𝑚1subscript𝑠0subscript𝜋0𝑠𝜋\langle s_{0},\pi_{0}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}^{m-1}\langle s,\pi\rangle. Consider now the step s0,π0kt,πsubscriptsubscript𝑘subscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\rightharpoonup_{{\mathcal{R}}_{k}}\langle t,\pi^{\prime}\rangle. Thus, there exist a position p𝒫𝗈𝗌(s0)𝑝𝒫𝗈𝗌subscript𝑠0p\in\mathsf{{{\cal P}}os}(s_{0}), a rule β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a ground substitution σ𝜎\sigma such that s0|p=lσevaluated-atsubscript𝑠0𝑝𝑙𝜎s_{0}|_{p}=l\sigma, siσ,[]kitiσ,πisuperscriptsubscriptsubscriptsubscript𝑘𝑖subscript𝑠𝑖𝜎subscript𝑡𝑖𝜎subscript𝜋𝑖\langle s_{i}\sigma,[\,]\rangle\rightharpoonup_{{\mathcal{R}}_{k_{i}}}^{\ast}\langle t_{i}\sigma,\pi_{i}\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n, t=s0[rσ]p𝑡subscript𝑠0subscriptdelimited-[]𝑟𝜎𝑝t=s_{0}[r\sigma]_{p}, σ=σ|`(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}})}, and π=β(p,σ,π1,,πn):π0:superscript𝜋𝛽𝑝superscript𝜎subscript𝜋1subscript𝜋𝑛subscript𝜋0\pi^{\prime}=\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}):\pi_{0}. By definition of ksubscriptsubscript𝑘\rightharpoonup_{{\mathcal{R}}_{k}}, we have that ki<ksubscript𝑘𝑖𝑘k_{i}<k and, thus, (ki,m1)<(k,m2)subscript𝑘𝑖subscript𝑚1𝑘subscript𝑚2(k_{i},m_{1})<(k,m_{2}) for all i=1,,n𝑖1𝑛i=1,\ldots,n and for all m1,m2subscript𝑚1subscript𝑚2m_{1},m_{2}. Hence, by the induction hypothesis, we have tiσ,πikisiσ,[]superscriptsubscriptsubscriptsubscript𝑘𝑖subscript𝑡𝑖𝜎subscript𝜋𝑖subscript𝑠𝑖𝜎\langle t_{i}\sigma,\pi_{i}\rangle\leftharpoondown_{{\mathcal{R}}_{k_{i}}}^{\ast}\langle s_{i}\sigma,[\,]\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n. Let θ=σ|`𝒱𝖺𝗋(r,sn¯)\𝒟𝗈𝗆(σ)\theta=\sigma\!\mathrel{|\grave{}}_{\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}})\backslash\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})}, so that σ=θσ𝜎𝜃superscript𝜎\sigma=\theta\sigma^{\prime} and 𝒟𝗈𝗆(θ)𝒟𝗈𝗆(σ)=𝒟𝗈𝗆𝜃𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)\cap\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\varnothing. Therefore, we have t,πks0,π0subscriptsubscript𝑘𝑡superscript𝜋subscriptsuperscript𝑠0subscript𝜋0\langle t,\pi^{\prime}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}\langle s^{\prime}_{0},\pi_{0}\rangle with t|p=rθevaluated-at𝑡𝑝𝑟𝜃t|_{p}=r\theta, β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}} and s0=t[lθσ]p=t[lσ]p=s0subscriptsuperscript𝑠0𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝𝑡subscriptdelimited-[]𝑙𝜎𝑝subscript𝑠0s^{\prime}_{0}=t[l\theta\sigma^{\prime}]_{p}=t[l\sigma]_{p}=s_{0}, and the claim follows.

()(\Leftarrow) This direction proceeds in a similar way. We prove the claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the considered derivation. Since the base case is trivial, let us consider the inductive case (k,m)>(0,0)𝑘𝑚00(k,m)>(0,0). Consider a derivation t,πkm1s0,π0ks,πsuperscriptsubscriptsubscript𝑘𝑚1𝑡superscript𝜋subscript𝑠0subscript𝜋0subscriptsubscript𝑘𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}^{m-1}\langle s_{0},\pi_{0}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}\langle s,\pi\rangle whose associated product is (k,m)𝑘𝑚(k,m). By Proposition 19, both s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle and s,π𝑠𝜋\langle s,\pi\rangle are safe. By the induction hypothesis, since (k,m1)<(k,m)𝑘𝑚1𝑘𝑚(k,m-1)<(k,m), we have s0,π0km1t,πsuperscriptsubscriptsubscript𝑘𝑚1subscript𝑠0subscript𝜋0𝑡superscript𝜋\langle s_{0},\pi_{0}\rangle\rightharpoonup_{{\mathcal{R}}_{k}}^{m-1}\langle t,\pi^{\prime}\rangle. Consider now the step s0,π0ks,πsubscriptsubscript𝑘subscript𝑠0subscript𝜋0𝑠𝜋\langle s_{0},\pi_{0}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}\langle s,\pi\rangle. Then, we have π0=β(p,σ,π1,,πn):π:subscript𝜋0𝛽𝑝superscript𝜎subscript𝜋1subscript𝜋𝑛𝜋\pi_{0}=\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}):\pi, β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and there exists a ground substitution θ𝜃\theta with 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r,sn¯)\𝒟𝗈𝗆(σ)𝒟𝗈𝗆𝜃\𝒱𝖺𝗋𝑟¯subscript𝑠𝑛𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}})\backslash\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime}) such that s0|p=rθevaluated-atsubscript𝑠0𝑝𝑟𝜃s_{0}|_{p}=r\theta, tiθσ,πikisiθσ,[]superscriptsubscriptsubscriptsubscript𝑘𝑖subscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖subscript𝑠𝑖𝜃superscript𝜎\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle\leftharpoondown_{{\mathcal{R}}_{k_{i}}}^{\ast}\langle s_{i}\theta\sigma^{\prime},[\,]\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n, and s=s0[lθσ]p𝑠subscript𝑠0subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s=s_{0}[l\theta\sigma^{\prime}]_{p}. Moreover, since s0,π0subscript𝑠0subscript𝜋0\langle s_{0},\pi_{0}\rangle is safe, we have that 𝒟𝗈𝗆(σ)=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}). By definition of ksubscriptsubscript𝑘\leftharpoondown_{{\mathcal{R}}_{k}}, we have that ki<ksubscript𝑘𝑖𝑘k_{i}<k and, thus, (ki,m1)<(k,m2)subscript𝑘𝑖subscript𝑚1𝑘subscript𝑚2(k_{i},m_{1})<(k,m_{2}) for all i=1,,n𝑖1𝑛i=1,\ldots,n and for all m1,m2subscript𝑚1subscript𝑚2m_{1},m_{2}. By the induction hypothesis, we have siθσ,[]kitiθσ,πisuperscriptsubscriptsubscriptsubscript𝑘𝑖subscript𝑠𝑖𝜃superscript𝜎subscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖\langle s_{i}\theta\sigma^{\prime},[\,]\rangle\rightharpoonup_{{\mathcal{R}}_{k_{i}}}^{\ast}\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n. Let σ=θσ𝜎𝜃superscript𝜎\sigma=\theta\sigma^{\prime}, with 𝒟𝗈𝗆(θ)𝒟𝗈𝗆(σ)=𝒟𝗈𝗆𝜃𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)\cap\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=\varnothing. Then, since s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma, we can perform the step s,πks0,β(p,σ,π1,,πn):π\langle s,\pi\rangle\rightharpoonup_{{\mathcal{R}}_{k}}\langle s^{\prime}_{0},\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}):\pi\rangle with s0=s[rσ]p=s[rθσ]psubscriptsuperscript𝑠0𝑠subscriptdelimited-[]𝑟𝜎𝑝𝑠subscriptdelimited-[]𝑟𝜃superscript𝜎𝑝s^{\prime}_{0}=s[r\sigma]_{p}=s[r\theta\sigma^{\prime}]_{p}; moreover, s[rθσ]p=s[rθ]p=s0[rθ]p=s0𝑠subscriptdelimited-[]𝑟𝜃superscript𝜎𝑝𝑠subscriptdelimited-[]𝑟𝜃𝑝subscript𝑠0subscriptdelimited-[]𝑟𝜃𝑝subscript𝑠0s[r\theta\sigma^{\prime}]_{p}=s[r\theta]_{p}=s_{0}[r\theta]_{p}=s_{0} since 𝒟𝗈𝗆(σ)𝒱𝖺𝗋(r)=𝒟𝗈𝗆superscript𝜎𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})\cap\mathsf{{{\mathcal{V}}}ar}(r)=\varnothing, which concludes the proof. \qed

In the following, we say that t,πs,πsubscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s,\pi\rangle is a deterministic step if there is no other, different pair s′′,π′′superscript𝑠′′superscript𝜋′′\langle s^{\prime\prime},\pi^{\prime\prime}\rangle with t,πs′′,π′′subscript𝑡superscript𝜋superscript𝑠′′superscript𝜋′′\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}\langle s^{\prime\prime},\pi^{\prime\prime}\rangle and, moreover, the subderivations for the equations in the condition of the applied rule (if any) are deterministic, too. We say that a derivation t,πs,πsuperscriptsubscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown_{\mathcal{R}}^{\ast}\langle s,\pi\rangle is deterministic if each reduction step in the derivation is deterministic.

Now, we can already prove that backward reversible rewriting is also deterministic, as in the unconditional case:

Theorem 21

Let {\mathcal{R}} be a DCTRS. Let t,π𝑡superscript𝜋\langle t,\pi^{\prime}\rangle be a safe pair with t,πs,πsubscriptsuperscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown^{\ast}_{\mathcal{R}}\langle s,\pi\rangle for some term s𝑠s and trace π𝜋\pi. Then t,πs,πsubscriptsuperscript𝑡superscript𝜋𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown^{\ast}_{\mathcal{R}}\langle s,\pi\rangle is deterministic.

Proof 6

We prove the claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the steps. The case m=0𝑚0m=0 is trivial, and thus we let m>0𝑚0m>0. Assume t,πkm1u,π′′ks,πsubscriptsuperscript𝑚1subscript𝑘𝑡superscript𝜋𝑢superscript𝜋′′subscriptsubscript𝑘𝑠𝜋\langle t,\pi^{\prime}\rangle\leftharpoondown^{m-1}_{{\mathcal{R}}_{k}}\langle u,\pi^{\prime\prime}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}\langle s,\pi\rangle. For the base case k=1𝑘1k=1, the applied rule is unconditional and the proof is analogous to that of Theorem 11.

Let us now consider k>1𝑘1k>1. By definition, if u,π′′ks,πsubscriptsubscript𝑘𝑢superscript𝜋′′𝑠𝜋\langle u,\pi^{\prime\prime}\rangle\leftharpoondown_{{\mathcal{R}}_{k}}\langle s,\pi\rangle, we have π′′=β(p,σ,π1,,πn):π:superscript𝜋′′𝛽𝑝superscript𝜎subscript𝜋1subscript𝜋𝑛𝜋\pi^{\prime\prime}=\beta(p,\sigma^{\prime},\pi_{1},\ldots,\pi_{n}):\pi, β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}} and there exists a ground substitution θ𝜃\theta with 𝒟𝗈𝗆(θ)=𝒱𝖺𝗋(r)𝒟𝗈𝗆𝜃𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{D}}}om}(\theta)=\mathsf{{{\mathcal{V}}}ar}(r) such that u|p=rθevaluated-at𝑢𝑝𝑟𝜃u|_{p}=r\theta, tiθσ,πijsiθσ,[]superscriptsubscriptsubscript𝑗subscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖subscript𝑠𝑖𝜃superscript𝜎\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle\leftharpoondown_{{\mathcal{R}}_{j}}^{\ast}\langle s_{i}\theta\sigma^{\prime},[\,]\rangle, j<k𝑗𝑘j<k, for all i=1,,n𝑖1𝑛i=1,\ldots,n, and s=t[lθσ]p𝑠𝑡subscriptdelimited-[]𝑙𝜃superscript𝜎𝑝s=t[l\theta\sigma^{\prime}]_{p}. By the induction hypothesis, the subderivations tiθσ,πijsiθσ,[]superscriptsubscriptsubscript𝑗subscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖subscript𝑠𝑖𝜃superscript𝜎\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle\leftharpoondown_{{\mathcal{R}}_{j}}^{\ast}\langle s_{i}\theta\sigma^{\prime},[\,]\rangle are deterministic, i.e., siθσ,[]subscript𝑠𝑖𝜃superscript𝜎\langle s_{i}\theta\sigma^{\prime},[\,]\rangle is a unique resulting term obtained by reducing tiθσ,πisubscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle. Therefore, the only remaining source of nondeterminism can come from choosing a rule labeled with β𝛽\beta and from the computed substitution θ𝜃\theta. On the one hand, the labels are unique in {\mathcal{R}}. As for θ𝜃\theta, we prove that this is indeed the only possible substitution for the reduction step. Consider the instance of rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}} with σsuperscript𝜎\sigma^{\prime}: lσrσsnσtnσ¯𝑙superscript𝜎𝑟superscript𝜎¯subscript𝑠𝑛superscript𝜎subscript𝑡𝑛superscript𝜎l\sigma^{\prime}\to r\sigma^{\prime}\Leftarrow\overline{s_{n}\sigma^{\prime}\twoheadrightarrow t_{n}\sigma^{\prime}}. Since u,π′′𝑢superscript𝜋′′\langle u,\pi^{\prime\prime}\rangle is safe, we have that σsuperscript𝜎\sigma^{\prime} is a ground substitution and 𝒟𝗈𝗆(σ)=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}). Then, the following properties hold:

  • 1.

    𝒱𝖺𝗋(lσ)𝒱𝖺𝗋(rσ,snσ¯,tnσ¯)𝒱𝖺𝗋𝑙superscript𝜎𝒱𝖺𝗋𝑟superscript𝜎¯subscript𝑠𝑛superscript𝜎¯subscript𝑡𝑛superscript𝜎\mathsf{{{\mathcal{V}}}ar}(l\sigma^{\prime})\subseteq\mathsf{{{\mathcal{V}}}ar}(r\sigma^{\prime},\overline{s_{n}\sigma^{\prime}},\overline{t_{n}\sigma^{\prime}}), since σsuperscript𝜎\sigma^{\prime} is ground and it covers all the variables in 𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯)\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}).

  • 2.

    𝒱𝖺𝗋(tiσ)𝒱𝖺𝗋(rσ,si+1,nσ¯)𝒱𝖺𝗋subscript𝑡𝑖superscript𝜎𝒱𝖺𝗋𝑟superscript𝜎¯subscript𝑠𝑖1𝑛superscript𝜎\mathsf{{{\mathcal{V}}}ar}(t_{i}\sigma^{\prime})\subseteq\mathsf{{{\mathcal{V}}}ar}(r\sigma^{\prime},\overline{s_{i+1,n}\sigma^{\prime}}) for all i=1,,n𝑖1𝑛i=1,\ldots,n, since σsuperscript𝜎\sigma^{\prime} is ground and it covers all variables in i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}).

The above properties guarantee that a rule of the form rσlσtnσsnσ,,t1σs1σformulae-sequence𝑟superscript𝜎𝑙superscript𝜎subscript𝑡𝑛superscript𝜎subscript𝑠𝑛superscript𝜎subscript𝑡1superscript𝜎subscript𝑠1superscript𝜎r\sigma^{\prime}\to l\sigma^{\prime}\Leftarrow t_{n}\sigma^{\prime}\twoheadrightarrow s_{n}\sigma^{\prime},\ldots,t_{1}\sigma^{\prime}\twoheadrightarrow s_{1}\sigma^{\prime} can be seen as a rule of a DCTRS and, thus, there exists a deterministic procedure to compute θ𝜃\theta, which completes the proof. \qed

Therefore, subscript\leftharpoondown_{\mathcal{R}} is deterministic and confluent. Termination is trivially guaranteed for pairs with a finite trace since the trace’s length strictly decreases with every backward step.

4 Removing Positions from Traces

Once we have a feasible definition of reversible rewriting, there are two refinements that can be considered: i) reducing the size of the traces and ii) defining a reversibilization transformation so that standard rewriting becomes reversible in the transformed system. In this section, we consider the first problem, leaving the second one for the next section.

In principle, one could remove information from the traces by requiring certain conditions on the considered systems. For instance, requiring injective functions may help to remove rule labels from trace terms. Also, requiring non-erasing rules may help to remove the second component of trace terms (i.e., the substitutions). In this section, however, we deal with a more challenging topic: removing positions from traces. This is useful not only to reduce the size of the traces but it is also essential to define a reversibilization technique for DCTRSs in the next section.666We note that defining a transformation with traces that include positions would be a rather difficult task because positions are dynamic (i.e., they depend on the term being reduced) and thus would require a complex (and inefficient) system instrumentation. In particular, we aim at transforming a given DCTRS into one that fulfills some conditions that make storing positions unnecessary.

In the following, given a CTRS {\mathcal{R}}, we say that a term t𝑡t is basic [18] if it has the form f(tn¯)𝑓¯subscript𝑡𝑛f(\overline{t_{n}}) with f𝒟𝑓subscript𝒟f\in{\mathcal{D}}_{\mathcal{R}} a defined function symbol and tn¯𝒯(𝒞,𝒱)¯subscript𝑡𝑛𝒯subscript𝒞𝒱\overline{t_{n}}\in{\mathcal{T}}({\mathcal{C}}_{\mathcal{R}},{\mathcal{V}}) constructor terms. Furthermore, in the remainder of this paper, we assume that the right-hand sides of the equations in the conditions of the rules of a DCTRS are constructor terms. This is not a significant restriction since these terms cannot be reduced anyway (since we consider oriented equations in this paper), and still covers most practical examples.

Now, we introduce the following subclass of DCTRSs:

Definition 22 (pcDCTRS [30])

We say that a DCTRS {\mathcal{R}} is a pcDCTRS (“pc” stands for pure constructor) if, for each rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, we have that l𝑙l and sn¯¯subscript𝑠𝑛\overline{s_{n}} are basic terms and r𝑟r and tn¯¯subscript𝑡𝑛\overline{t_{n}} are constructor terms.

Pure constructor systems are called normalized systems in [3]. Also, they are mostly equivalent to the class IIIn of conditional systems in [8], where t1,,tnsubscript𝑡1subscript𝑡𝑛t_{1},\ldots,t_{n} are required to be ground unconditional normal forms instead.777Given a CTRS {\mathcal{R}}, we define u={lrlrsntn¯}subscript𝑢conditional-set𝑙𝑟𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛{\mathcal{R}}_{u}=\{l\to r\mid l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}\}. A term is an unconditional normal form in {\mathcal{R}}, if it is a normal form in usubscript𝑢{\mathcal{R}}_{u}.

In principle, any DCTRS with basic terms in the left-hand sides (i.e., a constructor DCTRS) and constructor terms in the right-hand sides of the equations of the rules can be transformed into a pcDCTRS by applying a few simple transformations: flattening and simplification of constructor conditions. Let us now consider each of these transformations separately. Roughly speaking, flattening involves transforming a term (occurring, e.g., in the right-hand side of a DCTRS or in the condition) with nested defined functions like 𝖿(𝗀(x))𝖿𝗀𝑥\mathsf{f}(\mathsf{g}(x)) into a term 𝖿(y)𝖿𝑦\mathsf{f}(y) and an (oriented) equation 𝗀(x)y𝗀𝑥𝑦\mathsf{g}(x)\twoheadrightarrow y, where y𝑦y is a fresh variable. Formally,

Definition 23 (flattening)

Let {\mathcal{R}} be a CTRS, R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}})\in{\mathcal{R}} be a rule and Rsuperscript𝑅R^{\prime} be a new rule either of the form lrs1t1,,si|pw,si[w]pti,,sntnformulae-sequence𝑙𝑟subscript𝑠1subscript𝑡1evaluated-atsubscript𝑠𝑖𝑝𝑤formulae-sequencesubscript𝑠𝑖subscriptdelimited-[]𝑤𝑝subscript𝑡𝑖subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow s_{1}\twoheadrightarrow t_{1},\ldots,s_{i}|_{p}\twoheadrightarrow w,s_{i}[w]_{p}\twoheadrightarrow t_{i},\ldots,s_{n}\twoheadrightarrow t_{n}, for some p𝒫𝗈𝗌(si)𝑝𝒫𝗈𝗌subscript𝑠𝑖p\in\mathsf{{{\cal P}}os}(s_{i}), 1in1𝑖𝑛1\leqslant i\leqslant n, or lr[w]qsntn¯,r|qwformulae-sequence𝑙𝑟subscriptdelimited-[]𝑤𝑞¯subscript𝑠𝑛subscript𝑡𝑛evaluated-at𝑟𝑞𝑤l\to r[w]_{q}\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}},r|_{q}\twoheadrightarrow w, for some q𝒫𝗈𝗌(r)𝑞𝒫𝗈𝗌𝑟q\in\mathsf{{{\cal P}}os}(r), where w𝑤w is a fresh variable.888The positions p,q𝑝𝑞p,q can be required to be different from ϵitalic-ϵ\epsilon, but this is not strictly necessary. Then, a CTRS superscript{\mathcal{R}}^{\prime} is obtained from {\mathcal{R}} by a flattening step if =(\{R}){R}superscript\𝑅superscript𝑅{\mathcal{R}}^{\prime}=({\mathcal{R}}\backslash\{R\})\cup\{R^{\prime}\}.

Note that, if an unconditional rule is non-erasing (i.e., 𝒱𝖺𝗋(l)𝒱𝖺𝗋(r)𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{V}}}ar}(l)\subseteq\mathsf{{{\mathcal{V}}}ar}(r) for a rule lr𝑙𝑟l\to r), any conditional rule obtained by flattening is trivially non-erasing too, according to the notion of non-erasingness for DCTRSs in [32].999Roughly, a DCTRS is considered non-erasing in [32] if its transformation into an unconditional TRS by an unraveling transformation gives rise to a non-erasing TRS.

Flattening is trivially complete since any flattening step can be undone by binding the fresh variable again to the selected subterm and, then, proceeding as in the original system. Soundness is more subtle though. In this work, we prove the correctness of flattening for arbitrary DCTRSs with respect to innermost rewriting. As usual, the innermost rewrite relation, in symbols, 𝗂subscriptsuperscript𝗂\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\mathcal{R}}, is defined as the smallest binary relation satisfying the following: given ground terms s,t𝒯()𝑠𝑡𝒯s,t\in{\mathcal{T}}({\mathcal{F}}), we have s𝗂tsubscriptsuperscript𝗂𝑠𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\mathcal{R}}t iff there exist a position p𝑝p in s𝑠s such that no proper subterms of s|pevaluated-at𝑠𝑝s|_{p} are reducible, a rewrite rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a normalized ground substitution σ𝜎\sigma such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma, siσ𝗂tiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t_{i}\sigma, for all i=1,,n𝑖1𝑛i=1,\ldots,n, and t=s[rσ]p𝑡𝑠subscriptdelimited-[]𝑟𝜎𝑝t=s[r\sigma]_{p}.

In order to prove the correctness of flattening, we state the following auxiliary lemma:

Lemma 24

Let {\mathcal{R}} be a DCTRS. Given terms s𝑠s and t𝑡t, with t𝑡t a normal form, and a position p𝒫𝗈𝗌(s)𝑝𝒫𝗈𝗌𝑠p\in\mathsf{{{\cal P}}os}(s), we have s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t iff s|p𝗂wσsuperscript𝗂evaluated-at𝑠𝑝superscriptsubscript𝑤𝜎s|_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>w\sigma and s[wσ]p𝗂tsuperscript𝗂𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscript𝑡s[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t, for some fresh variable w𝑤w and normalized substitution σ𝜎\sigma.

Proof 7

()(\Rightarrow) Let us consider an arbitrary position p𝒫𝗈𝗌(s)𝑝𝒫𝗈𝗌𝑠p\in\mathsf{{{\cal P}}os}(s). If s|pevaluated-at𝑠𝑝s|_{p} is normalized, the proof is straightforward. Otherwise, since we use innermost reduction (leftmost innermost, for simplicity), we can represent the derivation s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t as follows:

s[s|p]p𝗂s[s|p]p𝗂s[s′′]p𝗂tsuperscript𝗂𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscript𝗂superscriptsubscriptsuperscript𝑠subscriptdelimited-[]superscript𝑠′′𝑝superscript𝗂superscriptsubscript𝑡s[s|_{p}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[s|_{p}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[s^{\prime\prime}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t

where s′′superscript𝑠′′s^{\prime\prime} is a normal form and the subderivation s[s|p]p𝗂s[s|p]psuperscript𝗂𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝s[s|_{p}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[s|_{p}]_{p} reduces the leftmost innermost subterms that are to the left of s|pevaluated-at𝑠𝑝s|_{p} (if any). Then, by choosing σ={ws′′}𝜎maps-to𝑤superscript𝑠′′\sigma=\{w\mapsto s^{\prime\prime}\} we have s|p𝗂wσsuperscript𝗂evaluated-at𝑠𝑝superscriptsubscript𝑤𝜎s|_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>w\sigma (by mimicking the steps of s[s|p]p𝗂s[s′′]psuperscript𝗂superscript𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]superscript𝑠′′𝑝s^{\prime}[s|_{p}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[s^{\prime\prime}]_{p}), s[wσ]p𝗂s[wσ]psuperscript𝗂𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]𝑤𝜎𝑝s[w\sigma]_{p}{\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}}\>s^{\prime}[w\sigma]_{p} (by mimicking the steps of s[s|p]p𝗂s[s|p]psuperscript𝗂𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝s[s|_{p}]_{p}{\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}}\>s^{\prime}[s|_{p}]_{p}), and s[wσ]p𝗂tsuperscript𝗂superscript𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscript𝑡s^{\prime}[w\sigma]_{p}{\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}}\>t (by mimicking the steps of s[s′′]p𝗂tsuperscript𝗂superscript𝑠subscriptdelimited-[]superscript𝑠′′𝑝superscriptsubscript𝑡s^{\prime}[s^{\prime\prime}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t), which concludes the proof.

()(\Leftarrow) This direction is perfectly analogous to the previous case. We consider an arbitrary position p𝒫𝗈𝗌(s)𝑝𝒫𝗈𝗌𝑠p\in\mathsf{{{\cal P}}os}(s) such that s|pevaluated-at𝑠𝑝s|_{p} is not normalized (otherwise, the proof is trivial). Now, since derivations are innermost, we can consider that s[wσ]p𝗂tsuperscript𝗂𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscript𝑡s[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t is as follows: s[wσ]p𝗂s[wσ]p𝗂tsuperscript𝗂𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]𝑤𝜎𝑝superscript𝗂superscriptsubscript𝑡s[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t, where s[wσ]p𝗂s[wσ]psuperscript𝗂𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]𝑤𝜎𝑝s[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[w\sigma]_{p} reduces the innermost subterms to the left of s|pevaluated-at𝑠𝑝s|_{p}. Therefore, we have s[s|p]p𝗂s[s|p]psuperscript𝗂𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝s[s|_{p}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[s|_{p}]_{p} (by mimicking the steps of s[wσ]p𝗂s[wσ]psuperscript𝗂𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]𝑤𝜎𝑝s[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[w\sigma]_{p}), s[s|p]p𝗂s[s′′]psuperscript𝗂superscript𝑠subscriptdelimited-[]evaluated-at𝑠𝑝𝑝superscriptsubscriptsuperscript𝑠subscriptdelimited-[]superscript𝑠′′𝑝s^{\prime}[s|_{p}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>s^{\prime}[s^{\prime\prime}]_{p} (by mimicking the steps of s|p𝗂wσsuperscript𝗂evaluated-at𝑠𝑝superscriptsubscript𝑤𝜎s|_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>w\sigma, with σ={ws′′}𝜎maps-to𝑤superscript𝑠′′\sigma=\{w\mapsto s^{\prime\prime}\}), and s[s′′]p𝗂tsuperscript𝗂superscript𝑠subscriptdelimited-[]superscript𝑠′′𝑝superscriptsubscript𝑡s^{\prime}[s^{\prime\prime}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t (by mimicking the steps of s[wσ]p𝗂tsuperscript𝗂superscript𝑠subscriptdelimited-[]𝑤𝜎𝑝superscriptsubscript𝑡s^{\prime}[w\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t). \qed

The following theorem is an easy consequence of the previous lemma:

Theorem 25

Let {\mathcal{R}} be a DCTRS. If superscript{\mathcal{R}}^{\prime} is obtained from {\mathcal{R}} by a flattening step, then superscript{\mathcal{R}}^{\prime} is a DCTRS and, for all ground terms s,t𝑠𝑡s,t, with t𝑡t a normal form, we have s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t iff s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡superscripts\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t.

Proof 8

()(\Rightarrow) We prove the claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the derivation s𝗂tksuperscript𝗂𝑠superscriptsubscript𝑡subscript𝑘s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k}}\>t. Since the base case is trivial, we consider the inductive case (k,m)>(0,0)𝑘𝑚00(k,m)>(0,0). Assume that s𝗂tksuperscript𝗂𝑠superscriptsubscript𝑡subscript𝑘s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k}}\>t has the form s[lσ]uk𝗂s[rσ]u𝗂tksubscriptsuperscript𝗂subscript𝑘𝑠subscriptdelimited-[]𝑙𝜎𝑢𝑠subscriptdelimited-[]𝑟𝜎𝑢superscript𝗂superscriptsubscript𝑡subscript𝑘s[l\sigma]_{u}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{{\mathcal{R}}_{k}}\>s[r\sigma]_{u}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k}}\>t with lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}} and siσ𝗂tikiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖subscriptsubscript𝑘𝑖𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k_{i}}}\;t_{i}\sigma, ki<ksubscript𝑘𝑖𝑘k_{i}<k, i=1,,n𝑖1𝑛i=1,\ldots,n. If lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛superscriptl\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}^{\prime}, the claim follows directly by induction. Otherwise, we have that either lrs1t1,,si|pw,si[w]pti,,sntnformulae-sequence𝑙𝑟subscript𝑠1subscript𝑡1evaluated-atsubscript𝑠𝑖𝑝𝑤formulae-sequencesubscript𝑠𝑖subscriptdelimited-[]𝑤𝑝subscript𝑡𝑖subscript𝑠𝑛subscript𝑡𝑛superscriptl\to r\Leftarrow s_{1}\twoheadrightarrow t_{1},\ldots,s_{i}|_{p}\twoheadrightarrow w,s_{i}[w]_{p}\twoheadrightarrow t_{i},\ldots,s_{n}\twoheadrightarrow t_{n}\in{\mathcal{R}}^{\prime}, for some p𝒫𝗈𝗌(si)𝑝𝒫𝗈𝗌subscript𝑠𝑖p\in\mathsf{{{\cal P}}os}(s_{i}), 1in1𝑖𝑛1\leqslant i\leqslant n, or lr[w]qsntn¯,r|qwformulae-sequence𝑙𝑟subscriptdelimited-[]𝑤𝑞¯subscript𝑠𝑛subscript𝑡𝑛evaluated-at𝑟𝑞𝑤superscriptl\to r[w]_{q}\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}},r|_{q}\twoheadrightarrow w\in{\mathcal{R}}^{\prime}, for some q𝒫𝗈𝗌(r)𝑞𝒫𝗈𝗌𝑟q\in\mathsf{{{\cal P}}os}(r), where w𝑤w is a fresh variable. Consider first the case lrs1t1,,si|pw,si[w]pti,,sntnformulae-sequence𝑙𝑟subscript𝑠1subscript𝑡1evaluated-atsubscript𝑠𝑖𝑝𝑤formulae-sequencesubscript𝑠𝑖subscriptdelimited-[]𝑤𝑝subscript𝑡𝑖subscript𝑠𝑛subscript𝑡𝑛superscriptl\to r\Leftarrow s_{1}\twoheadrightarrow t_{1},\ldots,s_{i}|_{p}\twoheadrightarrow w,s_{i}[w]_{p}\twoheadrightarrow t_{i},\ldots,s_{n}\twoheadrightarrow t_{n}\in{\mathcal{R}}^{\prime}, for some p𝒫𝗈𝗌(si)𝑝𝒫𝗈𝗌subscript𝑠𝑖p\in\mathsf{{{\cal P}}os}(s_{i}), 1in1𝑖𝑛1\leqslant i\leqslant n. Since siσ𝗂tikiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖subscriptsubscript𝑘𝑖𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k_{i}}}\;t_{i}\sigma, ki<ksubscript𝑘𝑖𝑘k_{i}<k, i=1,,n𝑖1𝑛i=1,\ldots,n, by the induction hypothesis, we have siσ𝗂tiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖superscript𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\;t_{i}\sigma, i=1,,n𝑖1𝑛i=1,\ldots,n. By Lemma 24, there exists σ={ws}superscript𝜎maps-to𝑤superscript𝑠\sigma^{\prime}=\{w\mapsto s^{\prime}\} for some normal form ssuperscript𝑠s^{\prime} such that si|pσ=si|pσσ𝗂wkiσσ=wσevaluated-atsubscript𝑠𝑖𝑝𝜎evaluated-atsubscript𝑠𝑖𝑝𝜎superscript𝜎superscript𝗂superscriptsubscript𝑤subscriptsubscript𝑘𝑖𝜎superscript𝜎𝑤superscript𝜎s_{i}|_{p}\sigma=s_{i}|_{p}\sigma\sigma^{\prime}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k_{i}}}\>w\sigma\sigma^{\prime}=w\sigma^{\prime} and si[w]pσσ=siσ[wσ]p𝗂tikisubscript𝑠𝑖subscriptdelimited-[]𝑤𝑝𝜎superscript𝜎subscript𝑠𝑖𝜎subscriptdelimited-[]𝑤superscript𝜎𝑝superscript𝗂superscriptsubscriptsubscript𝑡𝑖subscriptsubscript𝑘𝑖s_{i}[w]_{p}\sigma\sigma^{\prime}=s_{i}\sigma[w\sigma^{\prime}]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k_{i}}}t_{i}. Moreover, since w𝑤w is an extra variable, we also have sjσσ=sjσ𝗂tjσ=tjσσsubscript𝑠𝑗𝜎superscript𝜎subscript𝑠𝑗𝜎superscript𝗂superscriptsubscriptsubscript𝑡𝑗superscript𝜎subscript𝑡𝑗𝜎superscript𝜎s_{j}\sigma\sigma^{\prime}=s_{j}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t_{j}\sigma=t_{j}\sigma\sigma^{\prime} for j=1,,i1,i+1,,n𝑗1𝑖1𝑖1𝑛j=1,\ldots,i-1,i+1,\ldots,n. Therefore, since lσσ=lσ𝑙𝜎superscript𝜎𝑙𝜎l\sigma\sigma^{\prime}=l\sigma and rσσ=rσ𝑟𝜎superscript𝜎𝑟𝜎r\sigma\sigma^{\prime}=r\sigma, we have s[lσ]u𝗂s[rσ]usubscriptsuperscript𝗂𝑠subscriptdelimited-[]𝑙𝜎𝑢𝑠subscriptdelimited-[]𝑟𝜎𝑢s[l\sigma]_{u}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{{\mathcal{R}}}s[r\sigma]_{u}, and the claim follows by induction. Consider the second case. By the induction hypothesis, we have s[rσ]u𝗂tsuperscript𝗂𝑠subscriptdelimited-[]𝑟𝜎𝑢superscriptsubscript𝑡superscripts[r\sigma]_{u}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t and siσ𝗂tiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖superscript𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t_{i}\sigma for all i=1,,n𝑖1𝑛i=1,\ldots,n. By Lemma 24, there exists a substitution σ={ws}superscript𝜎maps-to𝑤superscript𝑠\sigma^{\prime}=\{w\mapsto s^{\prime}\} such that ssuperscript𝑠s^{\prime} is the normal form of r|qσevaluated-at𝑟𝑞𝜎r|_{q}\sigma and we have r|qσ𝗂wσsuperscript𝗂evaluated-at𝑟𝑞𝜎superscriptsubscript𝑤superscriptsuperscript𝜎r|_{q}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>w\sigma^{\prime} and s[rσ[wσ]q]u𝗂tsuperscript𝗂𝑠subscriptdelimited-[]𝑟𝜎subscriptdelimited-[]𝑤superscript𝜎𝑞𝑢superscriptsubscript𝑡superscripts[r\sigma[w\sigma^{\prime}]_{q}]_{u}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t. Moreover, since w𝑤w is a fresh variable, we have siσσ𝗂tiσσsuperscript𝗂subscript𝑠𝑖𝜎superscript𝜎superscriptsubscriptsubscript𝑡𝑖superscript𝜎superscript𝜎s_{i}\sigma\sigma^{\prime}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t_{i}\sigma\sigma^{\prime} for all i=1,,n𝑖1𝑛i=1,\ldots,n. Therefore, we have s[lσσ]u=s[lσ]u𝗂s[rσ[wσ]q]u𝑠subscriptdelimited-[]𝑙𝜎superscript𝜎𝑢𝑠subscriptdelimited-[]𝑙𝜎𝑢subscriptsuperscript𝗂superscript𝑠subscriptdelimited-[]𝑟𝜎subscriptdelimited-[]𝑤superscript𝜎𝑞𝑢s[l\sigma\sigma^{\prime}]_{u}=s[l\sigma]_{u}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{{\mathcal{R}}^{\prime}}s[r\sigma[w\sigma^{\prime}]_{q}]_{u}, which concludes the proof.

()(\Leftarrow) This direction is perfectly analogous to the previous one, and follows easily by Lemma 24 too. \qed

Let us now consider the second kind of transformations: the simplification of constructor conditions. Basically, we can drop an equation st𝑠𝑡s\twoheadrightarrow t when the terms s𝑠s and t𝑡t are constructor, called a constructor condition, by either applying the most general unifier (mgu) of s𝑠s and t𝑡t (if it exists) to the remaining part of the rule, or by deleting entirely the rule if they do not unify because (under innermost rewriting) the equation will never be satisfied by any normalized substitution. Similar transformations can be found in [33].

In order to justify these transformations, we state and prove the following results. In the following, we let mgu(s,t)𝑚𝑔𝑢𝑠𝑡mgu(s,t) denote the most general unifier of terms s𝑠s and t𝑡t if it exists, and 𝑓𝑎𝑖𝑙𝑓𝑎𝑖𝑙\mathit{fail} otherwise.

Theorem 26 (removal of unifiable constructor conditions)

Let {\mathcal{R}} be a DCTRS and let R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}})\in{\mathcal{R}} be a rule with mgu(si,ti)=θ𝑚𝑔𝑢subscript𝑠𝑖subscript𝑡𝑖𝜃mgu(s_{i},t_{i})=\theta, for some i{1,,n}𝑖1𝑛i\in\{1,\ldots,n\}, where sisubscript𝑠𝑖s_{i} and tisubscript𝑡𝑖t_{i} are constructor terms. Let Rsuperscript𝑅R^{\prime} be a new rule of the form lθrθs1θt1θ,,si1θti1θ,si+1θti+1θ,,snθtnθformulae-sequence𝑙𝜃𝑟𝜃subscript𝑠1𝜃subscript𝑡1𝜃subscript𝑠𝑖1𝜃subscript𝑡𝑖1𝜃formulae-sequencesubscript𝑠𝑖1𝜃subscript𝑡𝑖1𝜃subscript𝑠𝑛𝜃subscript𝑡𝑛𝜃l\theta\to r\theta\Leftarrow s_{1}\theta\twoheadrightarrow t_{1}\theta,\ldots,s_{i-1}\theta\twoheadrightarrow t_{i-1}\theta,s_{i+1}\theta\twoheadrightarrow t_{i+1}\theta,\ldots,s_{n}\theta\twoheadrightarrow t_{n}\theta.101010In [33], the condition 𝒟𝗈𝗆(θ)𝒱ar(l,r,s1,t1,,sn,tn)=𝒟𝗈𝗆𝜃𝒱𝑎𝑟𝑙𝑟subscript𝑠1subscript𝑡1subscript𝑠𝑛subscript𝑡𝑛\mathsf{{{\mathcal{D}}}om}(\theta)\cap{\mathcal{V}ar}(l,r,s_{1},t_{1},\ldots,s_{n},t_{n})=\varnothing is required, but this condition is not really necessary. Then =(\{R}){R}superscript\𝑅superscript𝑅{\mathcal{R}}^{\prime}=({\mathcal{R}}\backslash\{R\})\cup\{R^{\prime}\} is a DCTRS and, for all ground terms s𝑠s and t𝑡t, we have s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t iff s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡superscripts\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t.

Proof 9

()(\Rightarrow) First, we prove the following claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the steps: if s𝗂tkmsuperscript𝗂𝑠superscriptsubscript𝑡subscript𝑘𝑚s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{m}_{{\mathcal{R}}_{k}}\>t, then s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡superscripts\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t. It suffices to consider the case where R𝑅R is applied, i.e., s=s[lσ]p{R}𝗂s[rσ]p𝑠𝑠subscriptdelimited-[]𝑙𝜎𝑝subscriptsuperscript𝗂𝑅𝑠subscriptdelimited-[]𝑟𝜎𝑝s=s[l\sigma]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\{R\}}\>s[r\sigma]_{p} with sjσ𝗂tjkjσsuperscript𝗂subscript𝑠𝑗𝜎superscriptsubscriptsubscript𝑡𝑗subscriptsubscript𝑘𝑗𝜎s_{j}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}_{k_{j}}}\>t_{j}\sigma for all j{1,,n}𝑗1𝑛j\in\{1,\ldots,n\}. By definition, σ𝜎\sigma is normalized. Hence, since sisubscript𝑠𝑖s_{i} and tisubscript𝑡𝑖t_{i} are constructor terms, we have that siσsubscript𝑠𝑖𝜎s_{i}\sigma and tiσsubscript𝑡𝑖𝜎t_{i}\sigma are trivially normal forms since the normalized subterms introduced by σ𝜎\sigma cannot become reducible in a constructor context. Therefore, we have siσ=tiσsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎s_{i}\sigma=t_{i}\sigma. Thus, σ𝜎\sigma is a unifier of sisubscript𝑠𝑖s_{i} and tisubscript𝑡𝑖t_{i} and, hence, θ𝜃\theta is more general than σ𝜎\sigma. Let δ𝛿\delta be a substitution such that σ=θδ𝜎𝜃𝛿\sigma=\theta\delta. Since σ𝜎\sigma is normalized, so is δ𝛿\delta. Since kj<ksubscript𝑘𝑗𝑘k_{j}<k for all j=1,,n𝑗1𝑛j=1,\ldots,n, by the induction hypothesis, we have that sjσ=sjθδ𝗂tjθδ=tjσsubscript𝑠𝑗𝜎subscript𝑠𝑗𝜃𝛿superscript𝗂superscriptsubscriptsubscript𝑡𝑗superscript𝜃𝛿subscript𝑡𝑗𝜎s_{j}\sigma=s_{j}\theta\delta\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t_{j}\theta\delta=t_{j}\sigma for j{1,,i1,i+1,,n}𝑗1𝑖1𝑖1𝑛j\in\{1,\ldots,i-1,i+1,\ldots,n\}. Therefore, we have that s[lσ]p=s[lθδ]p{R}𝗂s[rθδ]p=s[rσ]p𝑠subscriptdelimited-[]𝑙𝜎𝑝𝑠subscriptdelimited-[]𝑙𝜃𝛿𝑝subscriptsuperscript𝗂superscript𝑅𝑠subscriptdelimited-[]𝑟𝜃𝛿𝑝𝑠subscriptdelimited-[]𝑟𝜎𝑝s[l\sigma]_{p}=s[l\theta\delta]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\{R^{\prime}\}}\>s[r\theta\delta]_{p}=s[r\sigma]_{p}.

()(\Leftarrow) Now, we prove the following claim by induction on the lexicographic product (k,m)𝑘𝑚(k,m) of the depth k𝑘k and the length m𝑚m of the steps: if s𝗂tkmsuperscript𝗂𝑠superscriptsubscript𝑡subscriptsuperscript𝑘𝑚s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{m}_{{\mathcal{R}}^{\prime}_{k}}\>t, then s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t. It suffices to consider the case where Rsuperscript𝑅R^{\prime} is applied, i.e., s=s[lθδ]p{R}𝗂s[rθδ]p𝑠𝑠subscriptdelimited-[]𝑙𝜃𝛿𝑝subscriptsuperscript𝗂𝑅𝑠subscriptdelimited-[]𝑟𝜃𝛿𝑝s=s[l\theta\delta]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\{R\}}\>s[r\theta\delta]_{p} with sjθδ𝗂tjkjθδsuperscript𝗂subscript𝑠𝑗𝜃𝛿superscriptsubscriptsubscript𝑡𝑗subscriptsuperscriptsubscript𝑘𝑗𝜃𝛿s_{j}\theta\delta\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}_{k_{j}}}\>t_{j}\theta\delta for all j{1,,i1,i+1,,n}𝑗1𝑖1𝑖1𝑛j\in\{1,\ldots,i-1,i+1,\ldots,n\}. By the assumption and the definition, θ𝜃\theta and δ𝛿\delta are normalized, and thus, siθδsubscript𝑠𝑖𝜃𝛿s_{i}\theta\delta and tiθδsubscript𝑡𝑖𝜃𝛿t_{i}\theta\delta are normal forms (as in the previous case, because the normalized subterms introduced by θδ𝜃𝛿\theta\delta cannot become reducible in a constructor context), i.e., siθδ=tiθδsubscript𝑠𝑖𝜃𝛿subscript𝑡𝑖𝜃𝛿s_{i}\theta\delta=t_{i}\theta\delta. Since kj<ksubscript𝑘𝑗𝑘k_{j}<k for all j{1,,i1,i+1,,n}𝑗1𝑖1𝑖1𝑛j\in\{1,\ldots,i-1,i+1,\ldots,n\}, by the induction hypothesis, we have that sjθδ𝗂tjθδsuperscript𝗂subscript𝑠𝑗𝜃𝛿superscriptsubscriptsubscript𝑡𝑗𝜃𝛿s_{j}\theta\delta\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}}\>t_{j}\theta\delta for j{1,,i1,i+1,,n}𝑗1𝑖1𝑖1𝑛j\in\{1,\ldots,i-1,i+1,\ldots,n\}. Therefore, we have that s[lσ]p=s[lθδ]p{R}𝗂s[rθδ]p=s[rσ]𝑠subscriptdelimited-[]𝑙𝜎𝑝𝑠subscriptdelimited-[]𝑙𝜃𝛿𝑝subscriptsuperscript𝗂𝑅𝑠subscriptdelimited-[]𝑟𝜃𝛿𝑝𝑠delimited-[]𝑟𝜎s[l\sigma]_{p}=s[l\theta\delta]_{p}\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\{R\}}\>s[r\theta\delta]_{p}=s[r\sigma] with σ=θδ𝜎𝜃𝛿\sigma=\theta\delta. \qed

Now we consider the case when the terms in the constructor condition do not unify:

Theorem 27 (removal of infeasible rules)

Let {\mathcal{R}} be a DCTRS and let R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}})\in{\mathcal{R}} be a rule with mgu(si,ti)=𝑓𝑎𝑖𝑙𝑚𝑔𝑢subscript𝑠𝑖subscript𝑡𝑖𝑓𝑎𝑖𝑙mgu(s_{i},t_{i})=\mathit{fail}, for some i{1,,n}𝑖1𝑛i\in\{1,\ldots,n\}. Then =\{R}superscript\𝑅{\mathcal{R}}^{\prime}={\mathcal{R}}\backslash\{R\} is a DCTRS and, for all ground terms s𝑠s and t𝑡t, we have s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t iff s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡superscripts\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t.

Proof 10

Since superscript{\mathcal{R}}\supseteq{\mathcal{R}}^{\prime}, the if part is trivial, and thus, we consider the only-if part. To apply R𝑅R to a term, there must exist a normalized substitution σ𝜎\sigma such that siσ𝗂tiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t_{i}\sigma. Since si,tisubscript𝑠𝑖subscript𝑡𝑖s_{i},t_{i} are constructor terms and σ𝜎\sigma is normalized, siσsubscript𝑠𝑖𝜎s_{i}\sigma is a normal form (because the normalized subterms introduced by σ𝜎\sigma cannot become reducible in a constructor context). If siσ𝗂tiσsuperscript𝗂subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t_{i}\sigma is satisfied (i.e., siσ=tiσsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎s_{i}\sigma=t_{i}\sigma), then sisubscript𝑠𝑖s_{i} and tisubscript𝑡𝑖t_{i} are unifiable, and thus, this contradicts the assumption. Therefore, R𝑅R is never applied to any term, and hence, s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t iff s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡superscripts\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t. \qed

Using flattening and the simplification of constructor conditions, any constructor DCTRS with constructor terms in the right-hand sides of the equations of the rules can be transformed into a pcDCTRS. One can use, for instance, the following simple algorithm. Let {\mathcal{R}} be such a constructor DCTRS. We apply the following transformations as much as possible:

(flattening-rhs)

Assume that {\mathcal{R}} contains a rule of the form R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}) where r𝑟r is not a constructor term. Let r|qevaluated-at𝑟𝑞r|_{q}, q𝒫𝗈𝗌(r)𝑞𝒫𝗈𝗌𝑟q\in\mathsf{{{\cal P}}os}(r), be a basic subterm of r𝑟r. Then, we replace rule R𝑅R by a new rule of the form lr[w]qsntn¯,r|qwformulae-sequence𝑙𝑟subscriptdelimited-[]𝑤𝑞¯subscript𝑠𝑛subscript𝑡𝑛evaluated-at𝑟𝑞𝑤l\to r[w]_{q}\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}},r|_{q}\twoheadrightarrow w, where w𝑤w is a fresh variable.

(flattening-condition)

Assume that {\mathcal{R}} contains a rule of the form R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}) where sisubscript𝑠𝑖s_{i} is neither a constructor term nor a basic term, i{1,,n}𝑖1𝑛i\in\{1,\ldots,n\}. Let si|qevaluated-atsubscript𝑠𝑖𝑞s_{i}|_{q}, q𝒫𝗈𝗌(s1)𝑞𝒫𝗈𝗌subscript𝑠1q\in\mathsf{{{\cal P}}os}(s_{1}), be a basic subterm of sisubscript𝑠𝑖s_{i}. Then, we replace rule R𝑅R by a new rule of the form lrs1t1,,si|qw,si[w]qti,,sntnformulae-sequence𝑙𝑟subscript𝑠1subscript𝑡1evaluated-atsubscript𝑠𝑖𝑞𝑤formulae-sequencesubscript𝑠𝑖subscriptdelimited-[]𝑤𝑞subscript𝑡𝑖subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow s_{1}\twoheadrightarrow t_{1},\ldots,s_{i}|_{q}\twoheadrightarrow w,s_{i}[w]_{q}\twoheadrightarrow t_{i},\ldots,s_{n}\twoheadrightarrow t_{n}, where w𝑤w is a fresh variable.

(removal-unify)

Assume that {\mathcal{R}} contains a rule of the form R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}) where sisubscript𝑠𝑖s_{i} is a constructor term, i{1,,n}𝑖1𝑛i\in\{1,\ldots,n\}. If mgu(si,ti)=θ𝑓𝑎𝑖𝑙𝑚𝑔𝑢subscript𝑠𝑖subscript𝑡𝑖𝜃𝑓𝑎𝑖𝑙mgu(s_{i},t_{i})=\theta\neq\mathit{fail}, then we replace rule R𝑅R by a new rule of the form lθrθs1θt1θ,,si1θti1θ,si+1θti+1θ,,snθtnθformulae-sequence𝑙𝜃𝑟𝜃subscript𝑠1𝜃subscript𝑡1𝜃subscript𝑠𝑖1𝜃subscript𝑡𝑖1𝜃formulae-sequencesubscript𝑠𝑖1𝜃subscript𝑡𝑖1𝜃subscript𝑠𝑛𝜃subscript𝑡𝑛𝜃l\theta\to r\theta\Leftarrow s_{1}\theta\twoheadrightarrow t_{1}\theta,\ldots,s_{i-1}\theta\twoheadrightarrow t_{i-1}\theta,s_{i+1}\theta\twoheadrightarrow t_{i+1}\theta,\ldots,s_{n}\theta\twoheadrightarrow t_{n}\theta.

(removal-fail)

Assume that {\mathcal{R}} contains a rule of the form R=(lrsntn¯)𝑅𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛R=(l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}) where sisubscript𝑠𝑖s_{i} is a constructor term, i{1,,n}𝑖1𝑛i\in\{1,\ldots,n\}. If mgu(si,ti)=𝑓𝑎𝑖𝑙𝑚𝑔𝑢subscript𝑠𝑖subscript𝑡𝑖𝑓𝑎𝑖𝑙mgu(s_{i},t_{i})=\mathit{fail}, then we remove rule R𝑅R from {\mathcal{R}}.

Trivially, by applying rule flattening-rhs as much as possible, we end up with a DCTRS where all the right-hand sides are constructor terms; analogously, the exhaustive application of rule flattening-condition allows us to ensure that the left-hand sides of all equations in the conditions of the rules are either constructor or basic; finally, the application of rules removal-unify and removal-fail produces a pcDCTRS by removing those equations in which the left-hand side is a constructor term. Therefore, in the remainder of this paper, we only consider pcDCTRSs.

A nice property of pcDCTRSs is that one can consider reductions only at topmost positions. Formally, given a pcDCTRS {\mathcal{R}}, we say that sp,lrsntn¯tsubscript𝑝𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛𝑠𝑡s\to_{p,l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}}t is a top reduction step if p=ϵ𝑝italic-ϵp=\epsilon, there is a ground substitution σ𝜎\sigma with s=lσ𝑠𝑙𝜎s=l\sigma, siσtiσsubscriptsuperscriptsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎s_{i}\sigma\to^{\ast}_{\mathcal{R}}t_{i}\sigma for all i=1,,n𝑖1𝑛i=1,\ldots,n, t=rσ𝑡𝑟𝜎t=r\sigma, and all the steps in siσtiσsubscriptsuperscriptsubscript𝑠𝑖𝜎subscript𝑡𝑖𝜎s_{i}\sigma\to^{\ast}_{\mathcal{R}}t_{i}\sigma for i=1,,n𝑖1𝑛i=1,\ldots,n are also top reduction steps. We denote top reductions with ϵsuperscriptitalic-ϵ\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}} for standard rewriting, and ϵ,ϵsubscriptsuperscriptitalic-ϵsubscriptsuperscriptitalic-ϵ\stackrel{{\scriptstyle\epsilon}}{{\rightharpoonup}}_{\mathcal{R}},\stackrel{{\scriptstyle\epsilon}}{{\leftharpoondown}}_{\mathcal{R}} for our reversible rewrite relations.

The following result basically states that 𝗂superscript𝗂\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}} and ϵsuperscriptitalic-ϵ\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}} are equivalent for pcDCTRSs:

Theorem 28

Let {\mathcal{R}} be a constructor DCTRS with constructor terms in the right-hand sides of the equations and superscript{\mathcal{R}}^{\prime} be a pcDCTRS obtained from {\mathcal{R}} by a sequence of transformations of flattening and simplification of constructor conditions. Given ground terms s𝑠s and t𝑡t such that s𝑠s is basic and t𝑡t is normalized, we have s𝗂tsuperscript𝗂𝑠superscriptsubscript𝑡s\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t iff sϵtsuperscriptitalic-ϵ𝑠superscriptsubscript𝑡superscripts\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}\!\!{}^{\ast}_{{\mathcal{R}}^{\prime}}\>t.

Proof 11

First, it is straightforward to see that an innermost reduction in superscript{\mathcal{R}}^{\prime} can only reduce the topmost positions of terms since defined functions can only occur at the root of terms and the terms introduced by instantiation are, by definition, irreducible. Therefore, the claim is a consequence of Theorems 25, 26 and 27, together with the above fact. \qed

Therefore, when considering pcDCTRSs and top reductions, storing the reduced positions in the trace terms becomes redundant since they are always ϵitalic-ϵ\epsilon. Thus, in practice, one can consider simpler trace terms without positions, β(σ,π1,,πn)𝛽𝜎subscript𝜋1subscript𝜋𝑛\beta(\sigma,\pi_{1},\ldots,\pi_{n}), that implicitly represent the trace term β(ϵ,σ,π1,,πn)𝛽italic-ϵ𝜎subscript𝜋1subscript𝜋𝑛\beta(\epsilon,\sigma,\pi_{1},\ldots,\pi_{n}).

Example 29

Consider the following TRS {\mathcal{R}} defining addition and multiplication on natural numbers, and its associated pcDCTRS superscript{\mathcal{R}}^{\prime}:

={𝖺𝖽𝖽(𝟢,y)y,𝖺𝖽𝖽(𝗌(x),y)𝗌(𝖺𝖽𝖽(x,y)),𝗆𝗎𝗅𝗍(𝟢,y)𝟢,𝗆𝗎𝗅𝗍(𝗌(x),y)𝖺𝖽𝖽(𝗆𝗎𝗅𝗍(x,y),y)}\begin{array}[]{l@{}r@{~}c@{~}l@{}l@{~}r@{~}c@{~}l}{\mathcal{R}}=\{&\mathsf{add}(\mathsf{0},y)~{}&\to\hfil~{}&y,\\ &\mathsf{add}(\mathsf{s}(x),y)~{}&\to\hfil~{}&\mathsf{s}(\mathsf{add}(x,y)),\\ &\mathsf{mult}(\mathsf{0},y)~{}&\to\hfil~{}&\mathsf{0},\\ &\mathsf{mult}(\mathsf{s}(x),y)~{}&\to\hfil~{}&\mathsf{add}(\mathsf{mult}(x,y),y)&\}\hfil~{}\end{array}
={𝖺𝖽𝖽(𝟢,y)y,𝖺𝖽𝖽(𝗌(x),y)𝗌(z)𝖺𝖽𝖽(x,y)z,𝗆𝗎𝗅𝗍(𝟢,y)𝟢,𝗆𝗎𝗅𝗍(𝗌(x),y)w𝗆𝗎𝗅𝗍(x,y)z,𝖺𝖽𝖽(z,y)w}\begin{array}[]{l@{}r@{~}c@{~}l@{}l@{~}r@{~}c@{~}l}{\mathcal{R}}^{\prime}=\{&\mathsf{add}(\mathsf{0},y)~{}&\to\hfil~{}&y,\\ &\mathsf{add}(\mathsf{s}(x),y)~{}&\to\hfil~{}&\mathsf{s}(z)\Leftarrow\mathsf{add}(x,y)\twoheadrightarrow z,\\ &\mathsf{mult}(\mathsf{0},y)~{}&\to\hfil~{}&\mathsf{0},\\ &\mathsf{mult}(\mathsf{s}(x),y)~{}&\to\hfil~{}&w\Leftarrow\mathsf{mult}(x,y)\twoheadrightarrow z,\mathsf{add}(z,y)\twoheadrightarrow w&\}\hfil~{}\\ \end{array}

For instance, given the following reduction in {\mathcal{R}}:

𝗆𝗎𝗅𝗍(𝗌(𝟢),𝗌(𝟢))𝗂𝖺𝖽𝖽(𝗆𝗎𝗅𝗍(𝟢,𝗌(𝟢)),𝗌(𝟢))𝗂𝖺𝖽𝖽(𝟢,𝗌(𝟢))𝗂𝗌(𝟢)subscriptsuperscript𝗂𝗆𝗎𝗅𝗍𝗌0𝗌0𝖺𝖽𝖽𝗆𝗎𝗅𝗍0𝗌0𝗌0subscriptsuperscript𝗂𝖺𝖽𝖽0𝗌0subscriptsuperscript𝗂𝗌0\mathsf{mult}(\mathsf{s}(\mathsf{0}),\mathsf{s}(\mathsf{0}))\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\mathcal{R}}\mathsf{add}(\mathsf{mult}(\mathsf{0},\mathsf{s}(\mathsf{0})),\mathsf{s}(\mathsf{0}))\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\mathcal{R}}\mathsf{add}(\mathsf{0},\mathsf{s}(\mathsf{0}))\stackrel{{\scriptstyle\mathsf{i}}}{{\rightarrow}}_{\mathcal{R}}\mathsf{s}(\mathsf{0})

we have the following counterpart in superscript{\mathcal{R}}^{\prime}:

𝗆𝗎𝗅𝗍(𝗌(𝟢),𝗌(𝟢))ϵ𝗌(𝟢)with𝗆𝗎𝗅𝗍(𝟢,𝗌(𝟢))ϵ𝟢and𝖺𝖽𝖽(𝟢,𝗌(𝟢))ϵ𝗌(𝟢)subscriptsuperscriptitalic-ϵsuperscript𝗆𝗎𝗅𝗍𝗌0𝗌0𝗌0withsubscriptsuperscriptitalic-ϵsuperscript𝗆𝗎𝗅𝗍0𝗌00missing-subexpressionandsubscriptsuperscriptitalic-ϵsuperscript𝖺𝖽𝖽0𝗌0𝗌0\begin{array}[]{lr@{~}l}\mathsf{mult}(\mathsf{s}(\mathsf{0}),\mathsf{s}(\mathsf{0}))\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{{\mathcal{R}}^{\prime}}\mathsf{s}(\mathsf{0})&\mbox{with}~{}&\mathsf{mult}(\mathsf{0},\mathsf{s}(\mathsf{0}))\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{{\mathcal{R}}^{\prime}}\mathsf{0}\\ &\mbox{and}~{}&\mathsf{add}(\mathsf{0},\mathsf{s}(\mathsf{0}))\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{{\mathcal{R}}^{\prime}}\mathsf{s}(\mathsf{0})\end{array}

Trivially, all results in Section 3 hold for pcDCTRSs and top reductions starting from basic terms. The simpler trace terms without positions will allow us to introduce appropriate injectivization and inversion transformations in the next section.

5 Reversibilization

In this section, we aim at compiling the reversible extension of rewriting into the system rules. Intuitively speaking, given a pure constructor system {\mathcal{R}}, we aim at producing new systems fsubscript𝑓{\mathcal{R}}_{f} and bsubscript𝑏{\mathcal{R}}_{b} such that standard rewriting in fsubscript𝑓{\mathcal{R}}_{f}, i.e., fsubscriptsubscript𝑓\to_{{\mathcal{R}}_{f}}, coincides with the forward reversible extension subscript\rightharpoonup_{\mathcal{R}} in the original system, and analogously bsubscriptsubscript𝑏\to_{{\mathcal{R}}_{b}} is equivalent to subscript\leftharpoondown_{\mathcal{R}}. Therefore, fsubscript𝑓{\mathcal{R}}_{f} can be seen as an injectivization of {\mathcal{R}}, and bsubscript𝑏{\mathcal{R}}_{b} as the inversion of fsubscript𝑓{\mathcal{R}}_{f}.

In principle, we could easily introduce a transformation for pcDCTRSs that mimicks the behavior of the reversible extension of rewriting. For instance, given the pcDCTRS {\mathcal{R}} of Example 16, we could produce the following injectivized version fsubscript𝑓{\mathcal{R}}_{f}:111111We will write just β𝛽\beta instead of β()𝛽\beta() when no argument is required.

𝖿(x,y,m),ws𝗌(w),β1(m,x,w1,w2):ws𝗁(x),[]x,w1,𝗀(y,𝟦),[]w,w2𝗁(𝟢),ws𝟢,β2:ws𝗁(𝟣),ws𝟣,β3:ws𝗀(x,y),wsx,β4(y):ws𝖿𝑥𝑦𝑚𝑤𝑠delimited-⟨⟩:𝗌𝑤subscript𝛽1𝑚𝑥subscript𝑤1subscript𝑤2𝑤𝑠missing-subexpressionmissing-subexpressionmissing-subexpressionformulae-sequence𝗁𝑥𝑥subscript𝑤1𝗀𝑦4𝑤subscript𝑤2missing-subexpression𝗁0𝑤𝑠delimited-⟨⟩:0subscript𝛽2𝑤𝑠missing-subexpression𝗁1𝑤𝑠delimited-⟨⟩:1subscript𝛽3𝑤𝑠missing-subexpression𝗀𝑥𝑦𝑤𝑠delimited-⟨⟩:𝑥subscript𝛽4𝑦𝑤𝑠missing-subexpression\begin{array}[]{@{}r@{~}c@{~}l@{~}l}\langle\mathsf{f}(x,y,m),ws\rangle~{}&\to\hfil~{}&\langle\mathsf{s}(w),\beta_{1}(m,x,w_{1},w_{2}):ws\rangle\hfil~{}\\ ~{}&\hfil~{}&\mbox{}\hskip 8.61108pt\Leftarrow\langle\mathsf{h}(x),[\,]\rangle\twoheadrightarrow\langle x,w_{1}\rangle,\langle\mathsf{g}(y,\mathsf{4}),[\,]\rangle\twoheadrightarrow\langle w,w_{2}\rangle\hfil~{}\\ \langle\mathsf{h}(\mathsf{0}),ws\rangle~{}&\to\hfil~{}&\langle\mathsf{0},\beta_{2}:ws\rangle\hfil~{}\\ \langle\mathsf{h}(\mathsf{1}),ws\rangle~{}&\to\hfil~{}&\langle\mathsf{1},\beta_{3}:ws\rangle\hfil~{}\\ \langle\mathsf{g}(x,y),ws\rangle~{}&\to\hfil~{}&\langle x,\beta_{4}(y):ws\rangle\hfil~{}\\ \end{array}

For instance, the reversible step 𝖿(𝟢,𝟤,𝟦),[]ϵ𝗌(𝟤),[β1(σ,π1,π2)]subscriptsuperscriptitalic-ϵ𝖿024𝗌2delimited-[]subscript𝛽1superscript𝜎subscript𝜋1subscript𝜋2\langle\mathsf{f}(\mathsf{0},\mathsf{2},\mathsf{4}),[\,]\rangle\stackrel{{\scriptstyle\epsilon}}{{\rightharpoonup}}_{\mathcal{R}}\langle\mathsf{s}(\mathsf{2}),[\beta_{1}(\sigma^{\prime},\pi_{1},\pi_{2})]\rangle with σ={m𝟦,x𝟢}superscript𝜎formulae-sequencemaps-to𝑚4maps-to𝑥0\sigma^{\prime}=\{m\mapsto\mathsf{4},x\mapsto\mathsf{0}\}, π1=[β2(id)]subscript𝜋1delimited-[]subscript𝛽2𝑖𝑑\pi_{1}=[\beta_{2}(id)] and π2=[β4({y4})]subscript𝜋2delimited-[]subscript𝛽4maps-to𝑦4\pi_{2}=[\beta_{4}(\{y\mapsto\textsf{4}\})], has the following counterpart in fsubscript𝑓{\mathcal{R}}_{f}:

𝖿(𝟢,𝟤,𝟦),[]fϵ𝗌(𝟤),[β1(𝟦,𝟢,[β2],[β4(𝟦)])]with𝗁(𝟢),[]fϵ𝟢,[β2]and𝗀(𝟤,𝟦),[]fϵ𝟤,[β4(𝟦)]subscriptsuperscriptitalic-ϵsubscript𝑓𝖿024𝗌2delimited-[]subscript𝛽140delimited-[]subscript𝛽2delimited-[]subscript𝛽44missing-subexpressionmissing-subexpressionsubscriptsuperscriptitalic-ϵsubscript𝑓with𝗁00delimited-[]subscript𝛽2and𝗀24subscriptsuperscriptitalic-ϵsubscript𝑓2delimited-[]subscript𝛽44missing-subexpressionmissing-subexpression\begin{array}[]{lll}\langle\mathsf{f}(\mathsf{0},\mathsf{2},\mathsf{4}),[\,]\rangle\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{{\mathcal{R}}_{f}}\langle\mathsf{s}(\mathsf{2}),[\beta_{1}(\mathsf{4},\mathsf{0},[\beta_{2}],[\beta_{4}(\mathsf{4})])]\rangle\\ \mbox{}\hskip 43.05542pt\mbox{with}~{}~{}\langle\mathsf{h}(\mathsf{0}),[\,]\rangle\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{{\mathcal{R}}_{f}}\langle\mathsf{0},[\beta_{2}]\rangle~{}~{}\mbox{and}~{}~{}\langle\mathsf{g}(\mathsf{2},\mathsf{4}),[\,]\rangle\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{{\mathcal{R}}_{f}}\langle\mathsf{2},[\beta_{4}(\mathsf{4})]\rangle\\ \end{array}

The only subtle difference here is that a trace term like

β1({m𝟦,x𝟢},[β2(id)],[β4({y4})])subscript𝛽1formulae-sequencemaps-to𝑚4maps-to𝑥0delimited-[]subscript𝛽2𝑖𝑑delimited-[]subscript𝛽4maps-to𝑦4\beta_{1}(\{m\mapsto\mathsf{4},x\mapsto\mathsf{0}\},[\beta_{2}(id)],[\beta_{4}(\{y\mapsto\textsf{4}\})])

is now stored in the transformed system as

β1(𝟦,𝟢,[β2],[β4(𝟦)])subscript𝛽140delimited-[]subscript𝛽2delimited-[]subscript𝛽44\beta_{1}(\mathsf{4},\mathsf{0},[\beta_{2}],[\beta_{4}(\mathsf{4})])

Furthermore, we could produce an inverse bsubscript𝑏{\mathcal{R}}_{b} of the above system as follows:

𝗌(w),β1(m,x,w1,w2):ws1𝖿(x,y,m),ws1w,w21𝗀(y,𝟦),[]1,x,w11𝗁(x),[]1𝟢,β2:ws1𝗁(𝟢),ws1𝟣,β3:ws1𝗁(𝟣),ws1x,β4(y):ws1𝗀(x,y),ws1\begin{array}[]{r@{~}c@{~}l@{~}l}\langle\mathsf{s}(w),\beta_{1}(m,x,w_{1},w_{2}):ws\rangle^{-1}~{}&\to\hfil~{}&\langle\mathsf{f}(x,y,m),ws\rangle^{-1}\hfil~{}\\ ~{}&\hfil~{}&\mbox{}\hskip 8.61108pt\Leftarrow\langle w,w_{2}\rangle^{-1}\twoheadrightarrow\langle\mathsf{g}(y,\mathsf{4}),[\,]\rangle^{-1},\hfil~{}\\ ~{}&\hfil~{}&\mbox{}\hskip 23.68048pt\langle x,w_{1}\rangle^{-1}\twoheadrightarrow\langle\mathsf{h}(x),[\,]\rangle^{-1}\hfil~{}\\ \langle\mathsf{0},\beta_{2}:ws\rangle^{-1}~{}&\to\hfil~{}&\langle\mathsf{h}(\mathsf{0}),ws\rangle^{-1}\hfil~{}\\ \langle\mathsf{1},\beta_{3}:ws\rangle^{-1}~{}&\to\hfil~{}&\langle\mathsf{h}(\mathsf{1}),ws\rangle^{-1}\hfil~{}\\ \langle x,\beta_{4}(y):ws\rangle^{-1}~{}&\to\hfil~{}&\langle\mathsf{g}(x,y),ws\rangle^{-1}\hfil~{}\\ \end{array}

mainly by switching the left- and right-hand sides of each rule and condition. The correctness of these injectivization and inversion transformations would be straightforward.

These transformations are only aimed at mimicking, step by step, the reversible relations subscript\rightharpoonup_{{\mathcal{R}}} and subscript\leftharpoondown_{{\mathcal{R}}}. Roughly speaking, for each step s,πt,πsubscript𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\rightharpoonup_{{\mathcal{R}}}\langle t,\pi^{\prime}\rangle in a system {\mathcal{R}}, we have s,πft,πsubscriptsubscript𝑓𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\to_{{\mathcal{R}}_{f}}\langle t,\pi^{\prime}\rangle, where fsubscript𝑓{\mathcal{R}}_{f} is the injectivized version of {\mathcal{R}}, and for each step s,πt,πsubscript𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\leftharpoondown_{{\mathcal{R}}}\langle t,\pi^{\prime}\rangle in {\mathcal{R}}, we have s,πbt,πsubscriptsubscript𝑏𝑠𝜋𝑡superscript𝜋\langle s,\pi\rangle\to_{{\mathcal{R}}_{b}}\langle t,\pi^{\prime}\rangle, where bsubscript𝑏{\mathcal{R}}_{b} is the inverse of fsubscript𝑓{\mathcal{R}}_{f}. More details on this approach can be found in [31]. Unfortunately, it might be much more useful to produce injective and inverse versions of each function defined in a system {\mathcal{R}}. Note that, in the above approach, the system fsubscript𝑓{\mathcal{R}}_{f} only defines a single function _,___\langle\_,\_\rangle and bsubscript𝑏{\mathcal{R}}_{b} only defines _,_1superscript__1\langle\_,\_\rangle^{-1}, i.e., we are computing systems that define the relations subscript\rightharpoonup_{\mathcal{R}} and subscript\leftharpoondown_{\mathcal{R}} rather than the injectivized and inverse versions of the functions in {\mathcal{R}}. In the following, we introduce more refined transformations that can actually produce injective and inverse versions of the original functions.

5.1 Injectivization

In principle, given a function 𝖿𝖿\mathsf{f}, one can consider that the injectivization of a rule of the form121212By abuse of notation, here we let s0¯,,sn¯¯subscript𝑠0¯subscript𝑠𝑛\overline{s_{0}},\ldots,\overline{s_{n}} denote sequences of terms of arbitrary length, i.e., s0¯=s0,1,,s0,l0¯subscript𝑠0subscript𝑠01subscript𝑠0subscript𝑙0\overline{s_{0}}=s_{0,1},\ldots,s_{0,l_{0}}, s1¯=s1,1,,s1,l1¯subscript𝑠1subscript𝑠11subscript𝑠1subscript𝑙1\overline{s_{1}}=s_{1,1},\ldots,s_{1,l_{1}}, etc.

β:𝖿(s0¯)r𝖿𝟣(s1¯)t1,,𝖿𝗇(sn¯)tn\beta:\mathsf{f}(\overline{s_{0}})\to r\Leftarrow\mathsf{f_{1}}(\overline{s_{1}})\twoheadrightarrow t_{1},\ldots,\mathsf{f_{n}}(\overline{s_{n}})\twoheadrightarrow t_{n}

produces the following rule

𝖿𝚒(s0¯)r,β(y¯,wn¯)𝖿1𝚒(s1¯)t1,w1,𝖿n𝚒(sn¯)tn,wnformulae-sequencesuperscript𝖿𝚒¯subscript𝑠0𝑟𝛽¯𝑦¯subscript𝑤𝑛superscriptsubscript𝖿1𝚒¯subscript𝑠1subscript𝑡1subscript𝑤1superscriptsubscript𝖿𝑛𝚒¯subscript𝑠𝑛subscript𝑡𝑛subscript𝑤𝑛\mathsf{f}^{\mathtt{i}}(\overline{s_{0}})\to\langle r,\beta(\overline{y},\overline{w_{n}})\rangle\Leftarrow\mathsf{f}_{1}^{\mathtt{i}}(\overline{s_{1}})\twoheadrightarrow\langle t_{1},w_{1}\rangle\ldots,\mathsf{f}_{n}^{\mathtt{i}}(\overline{s_{n}})\twoheadrightarrow\langle t_{n},w_{n}\rangle

where {y¯}=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)¯𝑦\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\{\overline{y}\}=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}) and wn¯¯subscript𝑤𝑛\overline{w_{n}} are fresh variables. The following example, though, illustrates that this is not correct in general.

Example 30

Consider the following pcDCTRS {\mathcal{R}}:

β1:𝖿(x,y)z𝗁(y)w,𝖿𝗂𝗋𝗌𝗍(x,w)zβ2:𝗁(𝟢)𝟢β3:𝖿𝗂𝗋𝗌𝗍(x,y)x:subscript𝛽1absent𝖿𝑥𝑦formulae-sequence𝑧𝗁𝑦𝑤𝖿𝗂𝗋𝗌𝗍𝑥𝑤𝑧:subscript𝛽2absent𝗁00:subscript𝛽3absent𝖿𝗂𝗋𝗌𝗍𝑥𝑦𝑥\begin{array}[]{lrcl}\beta_{1}:&\mathsf{f}(x,y)&\to&z\Leftarrow\mathsf{h}(y)\twoheadrightarrow w,~{}\mathsf{first}(x,w)\twoheadrightarrow z\\ \beta_{2}:&\mathsf{h}(\mathsf{0})&\to&\mathsf{0}\\ \beta_{3}:&\mathsf{first}(x,y)&\to&x\\ \end{array}

together with the following top reduction:

𝖿(𝟤,𝟣)ϵ𝟤withσ={x𝟤,y𝟣,w𝗁(𝟣),z𝟤}where𝗁(y)σ=𝗁(𝟣)ϵ𝗁(𝟣)=wσand𝖿𝗂𝗋𝗌𝗍(x,w)σ=𝖿𝗂𝗋𝗌𝗍(𝟤,𝗁(𝟣))ϵ𝟤=zσsubscriptsuperscriptitalic-ϵ𝖿212with𝜎formulae-sequencemaps-to𝑥2formulae-sequencemaps-to𝑦1formulae-sequencemaps-to𝑤𝗁1maps-to𝑧2missing-subexpressionwhere𝗁𝑦𝜎𝗁1superscriptitalic-ϵsuperscriptsubscript𝗁1𝑤𝜎missing-subexpressionand𝖿𝗂𝗋𝗌𝗍𝑥𝑤𝜎𝖿𝗂𝗋𝗌𝗍2𝗁1subscriptsuperscriptitalic-ϵ2𝑧𝜎\begin{array}[]{@{}ll@{}}\mathsf{f}(\mathsf{2},\mathsf{1})\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{\mathcal{R}}\mathsf{2}&\mbox{with}~{}\sigma=\{x\mapsto\mathsf{2},y\mapsto\mathsf{1},w\mapsto\mathsf{h}(\mathsf{1}),z\mapsto\mathsf{2}\}\\ &\mbox{where}~{}\mathsf{h}(y)\sigma=\mathsf{h}(\mathsf{1})\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>\mathsf{h}(\mathsf{1})=w\sigma\\ &\mbox{and}~{}\mathsf{first}(x,w)\sigma=\mathsf{first}(\mathsf{2},\mathsf{h}(\mathsf{1}))\stackrel{{\scriptstyle\mathsf{\epsilon}}}{{\rightarrow}}_{\mathcal{R}}\mathsf{2}=z\sigma\end{array}

Following the scheme above, we would produce the following pcDCTRS

𝖿𝚒(x,y)z,β1(w1,w2)𝗁𝚒(y)w,w1,𝖿𝗂𝗋𝗌𝗍𝚒(x,w)z,w2𝗁𝚒(𝟢)𝟢,β2𝖿𝗂𝗋𝗌𝗍𝚒(x,y)x,β3(y)superscript𝖿𝚒𝑥𝑦formulae-sequence𝑧subscript𝛽1subscript𝑤1subscript𝑤2superscript𝗁𝚒𝑦𝑤subscript𝑤1superscript𝖿𝗂𝗋𝗌𝗍𝚒𝑥𝑤𝑧subscript𝑤2superscript𝗁𝚒00subscript𝛽2superscript𝖿𝗂𝗋𝗌𝗍𝚒𝑥𝑦𝑥subscript𝛽3𝑦\begin{array}[]{r@{~}c@{~}l}\mathsf{f}^{\mathtt{i}}(x,y)~{}&\to\hfil~{}&\langle z,\beta_{1}(w_{1},w_{2})\rangle\Leftarrow\mathsf{h}^{\mathtt{i}}(y)\twoheadrightarrow\langle w,w_{1}\rangle,~{}\mathsf{first}^{\mathtt{i}}(x,w)\twoheadrightarrow\langle z,w_{2}\rangle\\ \mathsf{h}^{\mathtt{i}}(\mathsf{0})~{}&\to\hfil~{}&\langle\mathsf{0},\beta_{2}\rangle\\ \mathsf{first}^{\mathtt{i}}(x,y)~{}&\to\hfil~{}&\langle x,\beta_{3}(y)\rangle\\ \end{array}

Unfortunately, the corresponding reduction for 𝖿𝚒(𝟤,𝟣)superscript𝖿𝚒21\mathsf{f}^{\mathtt{i}}(\mathsf{2},\mathsf{1}) above cannot be done in this system since h𝚒(1)superscripth𝚒1\textsf{h}^{\mathtt{i}}(\textsf{1}) cannot be reduced to h𝚒(1),[]superscripth𝚒1\langle\textsf{h}^{\mathtt{i}}(\textsf{1}),[\,]\rangle.

In order to overcome this drawback, one could complete the function definitions with rules that reduce each irreducible term t𝑡t to a tuple of the form t,[]𝑡\langle t,[\,]\rangle. Although we find it a promising idea for future work, in this paper we propose a simpler approach. In the following, we consider a refinement of innermost reduction where only constructor substitutions are computed. Formally, the constructor reduction relation, 𝖼superscript𝖼\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}, is defined as follows: given ground terms s,t𝒯()𝑠𝑡𝒯s,t\in{\mathcal{T}}({\mathcal{F}}), we have s𝖼tsubscriptsuperscript𝖼𝑠𝑡s\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{\mathcal{R}}t iff there exist a position p𝑝p in s𝑠s such that no proper subterms of s|pevaluated-at𝑠𝑝s|_{p} are reducible, a rewrite rule lrsntn¯𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a ground constructor substitution σ𝜎\sigma such that s|p=lσevaluated-at𝑠𝑝𝑙𝜎s|_{p}=l\sigma, siσ𝖼tiσsuperscript𝖼subscript𝑠𝑖𝜎superscriptsubscriptsubscript𝑡𝑖𝜎s_{i}\sigma\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}\!\!{}^{\ast}_{\mathcal{R}}\>t_{i}\sigma for all i=1,,n𝑖1𝑛i=1,\ldots,n, and t=s[rσ]p𝑡𝑠subscriptdelimited-[]𝑟𝜎𝑝t=s[r\sigma]_{p}. Note that the results in the previous section also hold for 𝖼superscript𝖼\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}.

In the following, given a basic term t=𝖿(s¯)𝑡𝖿¯𝑠t=\mathsf{f}(\overline{s}), we denote by t𝚒superscript𝑡𝚒t^{\mathtt{i}} the term 𝖿𝚒(s¯)superscript𝖿𝚒¯𝑠\mathsf{f}^{\mathtt{i}}(\overline{s}). Now, we introduce our injectivization transformation as follows:

Definition 31 (injectivization)

Let {\mathcal{R}} be a pcDCTRS. We produce a new CTRS 𝐈()𝐈\mathbf{I}({\mathcal{R}}) by replacing each rule β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}} of {\mathcal{R}} by a new rule of the form

l𝚒r,β(y¯,wn¯)sn𝚒tn,wn¯superscript𝑙𝚒𝑟𝛽¯𝑦¯subscript𝑤𝑛¯superscriptsubscript𝑠𝑛𝚒subscript𝑡𝑛subscript𝑤𝑛l^{\mathtt{i}}\to\langle r,\beta(\overline{y},\overline{w_{n}})\rangle\Leftarrow\overline{s_{n}^{\mathtt{i}}\twoheadrightarrow\langle t_{n},w_{n}\rangle}

in 𝐈()𝐈\mathbf{I}({\mathcal{R}}), where {y¯}=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)¯𝑦\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\{\overline{y}\}=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}) and wn¯¯subscript𝑤𝑛\overline{w_{n}} are fresh variables. Here, we assume that the variables of y¯¯𝑦\overline{y} are in lexicographic order.

Observe that now we do not need to keep a trace in each term, but only a single trace term since all reductions finish in one step in a pcDCTRS. The relation between the original trace terms and the information stored in the injectivized system is formalized as follows:

Definition 32

Given a trace term π=β({ymtm¯},π1,,πn)𝜋𝛽¯maps-tosubscript𝑦𝑚subscript𝑡𝑚subscript𝜋1subscript𝜋𝑛\pi=\beta(\{\overline{y_{m}\mapsto t_{m}}\},\pi_{1},\ldots,\pi_{n}), we define π^^𝜋\widehat{\pi} recursively as follows: π^=β(tm¯,π1^,,πn^)^𝜋𝛽¯subscript𝑡𝑚^subscript𝜋1^subscript𝜋𝑛\widehat{\pi}=\beta(\overline{t_{m}},\widehat{\pi_{1}},\ldots,\widehat{\pi_{n}}), where we assume that the variables ym¯¯subscript𝑦𝑚\overline{y_{m}} are in lexicographic order.

Moreover, in order to simplify the notation, we consider that a a trace term π𝜋\pi and a singleton list of the form [π]delimited-[]𝜋[\pi] denote the same object. The correctness of the injectivization transformation is stated as follows:

Theorem 33

Let {\mathcal{R}} be a pcDCTRS and f=𝐈()subscript𝑓𝐈{\mathcal{R}}_{f}=\mathbf{I}({\mathcal{R}}) be its injectivization. Then fsubscript𝑓{\mathcal{R}}_{f} is a pcDCTRS and, given a basic ground term s𝑠s, we have s,[]𝖼t,πsubscriptsuperscript𝖼𝑠𝑡𝜋\langle s,[\,]\rangle\stackrel{{\scriptstyle\mathsf{c}}}{{\rightharpoonup}}_{\mathcal{R}}\langle t,\pi\rangle iff s𝚒f𝖼t,π^subscriptsuperscript𝖼subscript𝑓superscript𝑠𝚒𝑡^𝜋s^{\mathtt{i}}\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{f}}\langle t,\widehat{\pi}\rangle.

Proof 12

The fact that fsubscript𝑓{\mathcal{R}}_{f} is a pcDCTRS is trivial. Regarding the second part, we proceed as follows:

()(\Rightarrow) We proceed by induction on the depth k𝑘k of the step s,[]k𝖼t,πsubscriptsuperscript𝖼subscript𝑘𝑠𝑡𝜋\langle s,[\,]\rangle\stackrel{{\scriptstyle\mathsf{c}}}{{\rightharpoonup}}_{{\mathcal{R}}_{k}}\langle t,\pi\rangle. Since the depth k=0𝑘0k=0 is trivial, we consider the inductive case k>0𝑘0k>0. Thus, there is a rule β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}, and a substitution σ𝜎\sigma such that s=lσ𝑠𝑙𝜎s=l\sigma, siσ,[]𝖼tiσ,πiki\langle s_{i}\sigma,[\,]\rangle{\stackrel{{\scriptstyle\mathsf{c}}}{{\rightharpoonup}}\!\!{}_{{\mathcal{R}}_{k_{i}}}}\langle t_{i}\sigma,\pi_{i}\rangle, i=1,,n𝑖1𝑛i=1,\ldots,n, t=rσ𝑡𝑟𝜎t=r\sigma, σ=σ|`(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}})}, and π=β(σ,π1,,πn)𝜋𝛽superscript𝜎subscript𝜋1subscript𝜋𝑛\pi=\beta(\sigma^{\prime},\pi_{1},\ldots,\pi_{n}). By definition of ksubscriptsubscript𝑘\rightharpoonup_{{\mathcal{R}}_{k}}, we have that ki<ksubscript𝑘𝑖𝑘k_{i}<k for all i=1,,n𝑖1𝑛i=1,\ldots,n and, thus, by the induction hypothesis, we have (siσ)𝚒f𝖼tiσ,π^isubscriptsuperscript𝖼subscript𝑓superscriptsubscript𝑠𝑖𝜎𝚒subscript𝑡𝑖𝜎subscript^𝜋𝑖(s_{i}\sigma)^{\mathtt{i}}\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{f}}\>\langle t_{i}\sigma,\widehat{\pi}_{i}\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n. Consider now the equivalent rule in fsubscript𝑓{\mathcal{R}}_{f}: l𝚒r,β(y¯,wn¯)s1𝚒t1,w1,,sn𝚒tn,wnformulae-sequencesuperscript𝑙𝚒𝑟𝛽¯𝑦¯subscript𝑤𝑛superscriptsubscript𝑠1𝚒subscript𝑡1subscript𝑤1superscriptsubscript𝑠𝑛𝚒subscript𝑡𝑛subscript𝑤𝑛l^{\mathtt{i}}\to\langle r,\beta(\overline{y},\overline{w_{n}})\rangle\Leftarrow s_{1}^{\mathtt{i}}\twoheadrightarrow\langle t_{1},w_{1}\rangle,\ldots,s_{n}^{\mathtt{i}}\twoheadrightarrow\langle t_{n},w_{n}\rangle. Therefore, we have s𝚒f𝖼t,β(y¯σ,π^1,,π^n)subscriptsuperscript𝖼subscript𝑓superscript𝑠𝚒𝑡𝛽¯𝑦𝜎subscript^𝜋1subscript^𝜋𝑛s^{\mathtt{i}}\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{f}}\langle t,\beta(\overline{y}\sigma,\widehat{\pi}_{1},\ldots,\widehat{\pi}_{n})\rangle where {y¯}=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)¯𝑦\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\{\overline{y}\}=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}) and, thus, we can conclude that π^=β(y¯σ,π^1,,π^n)^𝜋𝛽¯𝑦𝜎subscript^𝜋1subscript^𝜋𝑛\widehat{\pi}=\beta(\overline{y}\sigma,\widehat{\pi}_{1},\ldots,\widehat{\pi}_{n}).

()(\Leftarrow) This direction is analogous. We proceed by induction on the depth k𝑘k of the step s𝚒fk𝖼t,π^subscriptsuperscript𝖼subscriptsubscript𝑓𝑘superscript𝑠𝚒𝑡^𝜋s^{\mathtt{i}}\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{f_{k}}}\>\langle t,\widehat{\pi}\rangle. Since the depth k=0𝑘0k=0 is trivial, we consider the inductive case k>0𝑘0k>0. Thus, there is a rule l𝚒r,β(y¯,wn¯)s1𝚒t1,w1,,sn𝚒tn,wnformulae-sequencesuperscript𝑙𝚒𝑟𝛽¯𝑦¯subscript𝑤𝑛superscriptsubscript𝑠1𝚒subscript𝑡1subscript𝑤1superscriptsubscript𝑠𝑛𝚒subscript𝑡𝑛subscript𝑤𝑛l^{\mathtt{i}}\to\langle r,\beta(\overline{y},\overline{w_{n}})\rangle\Leftarrow s_{1}^{\mathtt{i}}\twoheadrightarrow\langle t_{1},w_{1}\rangle,\ldots,s_{n}^{\mathtt{i}}\twoheadrightarrow\langle t_{n},w_{n}\rangle in fsubscript𝑓{\mathcal{R}}_{f} and a substitution θ𝜃\theta such that l𝚒θ=s𝚒superscript𝑙𝚒𝜃superscript𝑠𝚒l^{\mathtt{i}}\theta=s^{\mathtt{i}}, si𝚒θfki𝖼ti,wiθsubscriptsuperscript𝖼subscriptsubscript𝑓subscript𝑘𝑖subscriptsuperscript𝑠𝚒𝑖𝜃subscript𝑡𝑖subscript𝑤𝑖𝜃s^{\mathtt{i}}_{i}\theta\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{f_{k_{i}}}}\langle t_{i},w_{i}\rangle\theta, i=1,,n𝑖1𝑛i=1,\ldots,n, and r,β(y¯,wn¯)θ=t,π^𝑟𝛽¯𝑦¯subscript𝑤𝑛𝜃𝑡^𝜋\langle r,\beta(\overline{y},\overline{w_{n}})\rangle\theta=\langle t,\widehat{\pi}\rangle. Assume that σ𝜎\sigma is the restriction of θ𝜃\theta to the variables of the rule, excluding the fresh variables wn¯¯subscript𝑤𝑛\overline{w_{n}}, and that wiθ=π^isubscript𝑤𝑖𝜃subscript^𝜋𝑖w_{i}\theta=\widehat{\pi}_{i} for all i=1,,n𝑖1𝑛i=1,\ldots,n. Therefore, si,[]θ=siσ,[]subscript𝑠𝑖𝜃subscript𝑠𝑖𝜎\langle s_{i},[\,]\rangle\theta=\langle s_{i}\sigma,[\,]\rangle and ti,wiθ=tiσ,π^isubscript𝑡𝑖subscript𝑤𝑖𝜃subscript𝑡𝑖𝜎subscript^𝜋𝑖\langle t_{i},w_{i}\rangle\theta=\langle t_{i}\sigma,\widehat{\pi}_{i}\rangle, i=1,,n𝑖1𝑛i=1,\ldots,n. Then, by definition of fkisubscriptsubscript𝑓subscript𝑘𝑖{\mathcal{R}}_{f_{k_{i}}}, we have that ki<ksubscript𝑘𝑖𝑘k_{i}<k for all i=1,,n𝑖1𝑛i=1,\ldots,n and, thus, by the induction hypothesis, we have siσ,[]𝖼tiσ,πi\langle s_{i}\sigma,[\,]\rangle{\stackrel{{\scriptstyle\mathsf{c}}}{{\rightharpoonup}}\!\!{}_{{\mathcal{R}}}}\langle t_{i}\sigma,\pi_{i}\rangle, i=1,,n𝑖1𝑛i=1,\ldots,n. Consider now the equivalent rule in {\mathcal{R}}: β:lrsntn¯:𝛽𝑙𝑟¯subscript𝑠𝑛subscript𝑡𝑛\beta:l\to r\Leftarrow\overline{s_{n}\twoheadrightarrow t_{n}}\in{\mathcal{R}}. Therefore, we have s,[]𝖼t,πsubscriptsuperscript𝖼𝑠𝑡𝜋\langle s,[\,]\rangle\stackrel{{\scriptstyle\mathsf{c}}}{{\rightharpoonup}}_{\mathcal{R}}\langle t,\pi\rangle, σ=σ|`(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}})}, and π=β(σ,π1,,πn)𝜋𝛽superscript𝜎subscript𝜋1subscript𝜋𝑛\pi=\beta(\sigma^{\prime},\pi_{1},\ldots,\pi_{n}). Finally, since {y¯}=(𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1,n¯)¯𝑦\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1𝑛\{\overline{y}\}=(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1,n}}), we can conclude that π^=π^𝜋𝜋\widehat{\pi}=\pi. \qed

5.2 Inversion

Given an injectivized system, inversion basically amounts to switching the left- and right-hand sides of the rule and of every equation in the condition, as follows:

Definition 34 (inversion)

Let {\mathcal{R}} be a pcDCTRS and f=𝐈()subscript𝑓𝐈{\mathcal{R}}_{f}=\mathbf{I}({\mathcal{R}}) be its injectivization. The inverse system b=𝐈1(f)subscript𝑏superscript𝐈1subscript𝑓{\mathcal{R}}_{b}=\mathbf{I}^{-1}({\mathcal{R}}_{f}) is obtained from fsubscript𝑓{\mathcal{R}}_{f} by replacing each rule131313Here, we assume that s0¯¯subscript𝑠0\overline{s_{0}}, s1¯¯subscript𝑠1\overline{s_{1}},…, sn¯¯subscript𝑠𝑛\overline{s_{n}} denote arbitrary sequences of terms, i.e., s0¯=s0,1,,s0,l0¯subscript𝑠0subscript𝑠01subscript𝑠0subscript𝑙0\overline{s_{0}}=s_{0,1},\ldots,s_{0,l_{0}}, s1¯=s1,1,,s1,l1¯subscript𝑠1subscript𝑠11subscript𝑠1subscript𝑙1\overline{s_{1}}=s_{1,1},\ldots,s_{1,l_{1}}, etc. We use this notation for clarity.

𝖿𝚒(s0¯)r,β(y¯,wn¯)𝖿1𝚒(s1¯)t1,w1,,𝖿n𝚒(sn¯)tn,wnformulae-sequencesuperscript𝖿𝚒¯subscript𝑠0𝑟𝛽¯𝑦¯subscript𝑤𝑛superscriptsubscript𝖿1𝚒¯subscript𝑠1subscript𝑡1subscript𝑤1superscriptsubscript𝖿𝑛𝚒¯subscript𝑠𝑛subscript𝑡𝑛subscript𝑤𝑛\mathsf{f}^{\mathtt{i}}(\overline{s_{0}})\to\langle r,\beta(\overline{y},\overline{w_{n}})\rangle\Leftarrow\mathsf{f}_{1}^{\mathtt{i}}(\overline{s_{1}})\twoheadrightarrow\langle t_{1},w_{1}\rangle,\ldots,\mathsf{f}_{n}^{\mathtt{i}}(\overline{s_{n}})\twoheadrightarrow\langle t_{n},w_{n}\rangle

of fsubscript𝑓{\mathcal{R}}_{f} by a new rule of the form

𝖿1(r,β(y¯,wn¯))s0¯𝖿n1(tn,wn)sn¯,,𝖿11(t1,w1)s1¯formulae-sequencesuperscript𝖿1𝑟𝛽¯𝑦¯subscript𝑤𝑛delimited-⟨⟩¯subscript𝑠0superscriptsubscript𝖿𝑛1subscript𝑡𝑛subscript𝑤𝑛delimited-⟨⟩¯subscript𝑠𝑛superscriptsubscript𝖿11subscript𝑡1subscript𝑤1delimited-⟨⟩¯subscript𝑠1\mathsf{f}^{-1}(r,\beta(\overline{y},\overline{w_{n}}))\to\langle\overline{s_{0}}\rangle\Leftarrow\mathsf{f}_{n}^{-1}(t_{n},w_{n})\twoheadrightarrow\langle\overline{s_{n}}\rangle,\ldots,\mathsf{f}_{1}^{-1}(t_{1},w_{1})\twoheadrightarrow\langle\overline{s_{1}}\rangle

in 𝐈1(f)superscript𝐈1subscript𝑓\mathbf{I}^{-1}({\mathcal{R}}_{f}), where the variables of y¯¯𝑦\overline{y} are in lexicographic order.

Example 35

Consider again the pcDCTRS of Example 16. Here, injectivization returns the following pcDCTRS 𝐈()=f𝐈subscript𝑓\mathbf{I}({\mathcal{R}})={\mathcal{R}}_{f}:

𝖿𝚒(x,y,m)𝗌(w),β1(m,x,w1,w2)𝗁𝚒(x)x,w1,𝗀𝚒(y,𝟦)w,w2𝗁𝚒(𝟢)𝟢,β2𝗁𝚒(𝟣)𝟣,β3𝗀𝚒(x,y)x,β4(y)superscript𝖿𝚒𝑥𝑦𝑚𝗌𝑤subscript𝛽1𝑚𝑥subscript𝑤1subscript𝑤2missing-subexpressionmissing-subexpressionformulae-sequencesuperscript𝗁𝚒𝑥𝑥subscript𝑤1superscript𝗀𝚒𝑦4𝑤subscript𝑤2superscript𝗁𝚒00subscript𝛽2superscript𝗁𝚒11subscript𝛽3superscript𝗀𝚒𝑥𝑦𝑥subscript𝛽4𝑦\begin{array}[]{r@{~}c@{~}l}\mathsf{f}^{\mathtt{i}}(x,y,m)~{}&\to\hfil~{}&\langle\mathsf{s}(w),\beta_{1}(m,x,w_{1},w_{2})\rangle\\ ~{}&\hfil~{}&\mbox{}\hskip 8.61108pt\Leftarrow\mathsf{h}^{\mathtt{i}}(x)\twoheadrightarrow\langle x,w_{1}\rangle,\mathsf{g}^{\mathtt{i}}(y,\mathsf{4})\twoheadrightarrow\langle w,w_{2}\rangle\\ \mathsf{h}^{\mathtt{i}}(\mathsf{0})~{}&\to\hfil~{}&\langle\mathsf{0},\beta_{2}\rangle\\ \mathsf{h}^{\mathtt{i}}(\mathsf{1})~{}&\to\hfil~{}&\langle\mathsf{1},\beta_{3}\rangle\\ \mathsf{g}^{\mathtt{i}}(x,y)~{}&\to\hfil~{}&\langle x,\beta_{4}(y)\rangle\\ \end{array}

Then, inversion with 𝐈1superscript𝐈1\mathbf{I}^{-1} produces the following pcDCTRS 𝐈1(𝐈())=bsuperscript𝐈1𝐈subscript𝑏\mathbf{I}^{-1}(\mathbf{I}({\mathcal{R}}))={\mathcal{R}}_{b}:

𝖿1(𝗌(w),β1(m,x,w1,w2))x,y,m𝗀1(w,w2)y,𝟦,𝗁1(x,w1)x𝗁1(𝟢,β2)𝟢𝗁1(𝟣,β3)𝟣𝗀1(x,β4(y))x,ysuperscript𝖿1𝗌𝑤subscript𝛽1𝑚𝑥subscript𝑤1subscript𝑤2𝑥𝑦𝑚missing-subexpressionmissing-subexpressionformulae-sequencesuperscript𝗀1𝑤subscript𝑤2𝑦4superscript𝗁1𝑥subscript𝑤1delimited-⟨⟩𝑥superscript𝗁10subscript𝛽2delimited-⟨⟩0superscript𝗁11subscript𝛽3delimited-⟨⟩1superscript𝗀1𝑥subscript𝛽4𝑦𝑥𝑦\begin{array}[]{r@{~}c@{~}l}\mathsf{f}^{-1}(\mathsf{s}(w),\beta_{1}(m,x,w_{1},w_{2}))~{}&\to\hfil~{}&\langle x,y,m\rangle\\ ~{}&\hfil~{}&\mbox{}\hskip 8.61108pt\Leftarrow\mathsf{g}^{-1}(w,w_{2})\twoheadrightarrow\langle y,\mathsf{4}\rangle,\mathsf{h}^{-1}(x,w_{1})\twoheadrightarrow\langle x\rangle\\ \mathsf{h}^{-1}(\mathsf{0},\beta_{2})~{}&\to\hfil~{}&\langle\mathsf{0}\rangle\\ \mathsf{h}^{-1}(\mathsf{1},\beta_{3})~{}&\to\hfil~{}&\langle\mathsf{1}\rangle\\ \mathsf{g}^{-1}(x,\beta_{4}(y))~{}&\to\hfil~{}&\langle x,y\rangle\\ \end{array}

Finally, the correctness of the inversion transformation is stated as follows:

Theorem 36

Let {\mathcal{R}} be a pcDCTRS, f=𝐈()subscript𝑓𝐈{\mathcal{R}}_{f}=\mathbf{I}({\mathcal{R}}) its injectivization, and b=𝐈1(f)subscript𝑏superscript𝐈1subscript𝑓{\mathcal{R}}_{b}=\mathbf{I}^{-1}({\mathcal{R}}_{f}) the inversion of fsubscript𝑓{\mathcal{R}}_{f}. Then, bsubscript𝑏{\mathcal{R}}_{b} is a basic pcDCTRS and, given a basic ground term 𝖿(s¯)𝖿¯𝑠\mathsf{f}(\overline{s}) and a constructor ground term t𝑡t with t,π𝑡𝜋\langle t,\pi\rangle a safe pair, we have t,π𝖼𝖿(s¯),[]subscriptsuperscript𝖼𝑡𝜋𝖿¯𝑠\langle t,\pi\rangle\stackrel{{\scriptstyle\mathsf{c}}}{{\leftharpoondown}}_{\mathcal{R}}\langle\mathsf{f}(\overline{s}),[\,]\rangle iff 𝖿1(t,π^)b𝖼s¯subscriptsuperscript𝖼subscript𝑏superscript𝖿1𝑡^𝜋delimited-⟨⟩¯𝑠\mathsf{f}^{-1}(t,\widehat{\pi})\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{b}}\>\langle\overline{s}\rangle.

Proof 13

The fact that fsubscript𝑓{\mathcal{R}}_{f} is a pcDCTRS is trivial. Regarding the second part, we proceed as follows.

()(\Rightarrow) We proceed by induction on the depth k𝑘k of the step t,πk𝖼𝖿(s¯),[]subscriptsuperscript𝖼subscript𝑘𝑡𝜋𝖿¯𝑠\langle t,\pi\rangle\stackrel{{\scriptstyle\mathsf{c}}}{{\leftharpoondown}}_{{\mathcal{R}}_{k}}\langle\mathsf{f}(\overline{s}),[\,]\rangle. Since the depth k=0𝑘0k=0 is trivial, we consider the inductive case k>0𝑘0k>0. Let π=β(σ,πn¯)𝜋𝛽superscript𝜎¯subscript𝜋𝑛\pi=\beta(\sigma^{\prime},\overline{\pi_{n}}). Thus, we have that t,β(σ,πn¯)𝑡𝛽superscript𝜎¯subscript𝜋𝑛\langle t,\beta(\sigma^{\prime},\overline{\pi_{n}})\rangle is a safe pair, there is a rule β:𝖿(s0¯)r𝖿1(s1¯)t1,,𝖿n(sn¯)tn\beta:\mathsf{f}(\overline{s_{0}})\to r\Leftarrow\mathsf{f}_{1}(\overline{s_{1}})\twoheadrightarrow t_{1},\ldots,\mathsf{f}_{n}(\overline{s_{n}})\twoheadrightarrow t_{n} and substitution θ𝜃\theta with 𝒟𝗈𝗆(θ)=(𝒱𝖺𝗋(r,s1¯,,sn¯)\𝒟𝗈𝗆(σ))𝒟𝗈𝗆𝜃\𝒱𝖺𝗋𝑟¯subscript𝑠1¯subscript𝑠𝑛𝒟𝗈𝗆superscript𝜎\mathsf{{{\mathcal{D}}}om}(\theta)=(\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{1}},\ldots,\overline{s_{n}})\backslash\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})) such that t=rθ𝑡𝑟𝜃t=r\theta, tiθσ,πiki𝖼𝖿(si¯)θσ,[]subscriptsuperscript𝖼subscriptsubscript𝑘𝑖subscript𝑡𝑖𝜃superscript𝜎subscript𝜋𝑖𝖿¯subscript𝑠𝑖𝜃superscript𝜎\langle t_{i}\theta\sigma^{\prime},\pi_{i}\rangle{\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{k_{i}}}}\langle\mathsf{f}(\overline{s_{i}})\theta\sigma^{\prime},[\,]\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n, and 𝖿(s¯)=𝖿(s0¯)θσ𝖿¯𝑠𝖿¯subscript𝑠0𝜃superscript𝜎\mathsf{f}(\overline{s})=\mathsf{f}(\overline{s_{0}})\theta\sigma^{\prime}. Note that s0¯,,sn¯¯subscript𝑠0¯subscript𝑠𝑛\overline{s_{0}},\ldots,\overline{s_{n}} denote sequences of terms of arbitrary length, i.e., s0¯=s0,1,,s0,l0¯subscript𝑠0subscript𝑠01subscript𝑠0subscript𝑙0\overline{s_{0}}=s_{0,1},\ldots,s_{0,l_{0}}, s1¯=s1,1,,s1,l1¯subscript𝑠1subscript𝑠11subscript𝑠1subscript𝑙1\overline{s_{1}}=s_{1,1},\ldots,s_{1,l_{1}}, etc. Since t,π𝑡𝜋\langle t,\pi\rangle is a safe pair, we have that 𝒟𝗈𝗆(σ)=(𝒱𝖺𝗋(s0¯)\𝒱𝖺𝗋(r,s1¯,,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1¯,,sn¯)𝒟𝗈𝗆superscript𝜎\𝒱𝖺𝗋¯subscript𝑠0𝒱𝖺𝗋𝑟¯subscript𝑠1¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1¯subscript𝑠𝑛\mathsf{{{\mathcal{D}}}om}(\sigma^{\prime})=(\mathsf{{{\mathcal{V}}}ar}(\overline{s_{0}})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{1}},\ldots,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1}},\ldots,\overline{s_{n}}). By definition of ksubscriptsubscript𝑘\leftharpoondown_{{\mathcal{R}}_{k}}, we have that ki<ksubscript𝑘𝑖𝑘k_{i}<k for all i=1,,n𝑖1𝑛i=1,\ldots,n and, by the induction hypothesis, we have 𝖿1(tiσ,πi^)b𝖼si¯σsubscriptsuperscript𝖼subscript𝑏superscript𝖿1subscript𝑡𝑖𝜎^subscript𝜋𝑖delimited-⟨⟩¯subscript𝑠𝑖𝜎\mathsf{f}^{-1}(t_{i}\sigma,\widehat{\pi_{i}}){\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{b}}}\langle\overline{s_{i}}\sigma\rangle for all i=1,,n𝑖1𝑛i=1,\ldots,n. Let us now consider the equivalent rule in bsubscript𝑏{\mathcal{R}}_{b}:

𝖿1(r,β(y¯,wn¯)))s0¯𝖿n1(tn,wn)sn¯,,𝖿11(t1,w1)s1¯\mathsf{f}^{-1}(r,\beta(\overline{y},\overline{w_{n}})))\to\langle\overline{s_{0}}\rangle\Leftarrow\mathsf{f}_{n}^{-1}(t_{n},w_{n})\twoheadrightarrow\langle\overline{s_{n}}\rangle,\ldots,\mathsf{f}_{1}^{-1}(t_{1},w_{1})\twoheadrightarrow\langle\overline{s_{1}}\rangle

Hence, we have 𝖿1(t,β(y¯σ,π^1,,π^1))bs0¯σ=s¯subscriptsubscript𝑏superscript𝖿1𝑡𝛽¯𝑦𝜎subscript^𝜋1subscript^𝜋1delimited-⟨⟩¯subscript𝑠0𝜎delimited-⟨⟩¯𝑠\mathsf{f}^{-1}(t,\beta(\overline{y}\sigma,\widehat{\pi}_{1},\ldots,\widehat{\pi}_{1}))\to_{{\mathcal{R}}_{b}}\langle\overline{s_{0}}\sigma\rangle=\langle\overline{s}\rangle, where

{y¯}=(𝒱𝖺𝗋(s0¯)\𝒱𝖺𝗋(r,s1¯,,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1¯,,sn¯)¯𝑦\𝒱𝖺𝗋¯subscript𝑠0𝒱𝖺𝗋𝑟¯subscript𝑠1¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1¯subscript𝑠𝑛\{\overline{y}\}=(\mathsf{{{\mathcal{V}}}ar}(\overline{s_{0}})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{1}},\ldots,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1}},\ldots,\overline{s_{n}})

and, thus, we can conclude that π^=β(y¯σ,π^1,,π^n)^𝜋𝛽¯𝑦𝜎subscript^𝜋1subscript^𝜋𝑛\widehat{\pi}=\beta(\overline{y}\sigma,\widehat{\pi}_{1},\ldots,\widehat{\pi}_{n}).

()(\Leftarrow) This direction is analogous. We proceed by induction on the depth k𝑘k of the step 𝖿1(t,π^)bk𝖼s¯subscriptsuperscript𝖼subscriptsubscript𝑏𝑘superscript𝖿1𝑡^𝜋delimited-⟨⟩¯𝑠\mathsf{f}^{-1}(t,\widehat{\pi})\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{b_{k}}}\langle\overline{s}\rangle. Since the depth k=0𝑘0k=0 is trivial, we consider the inductive case k>0𝑘0k>0. Thus, there is a rule 𝖿1(r,β(y¯,wn¯)))s0¯𝖿n1(tn,wn)sn¯,,𝖿11(t1,w1)s1¯\mathsf{f}^{-1}(r,\beta(\overline{y},\overline{w_{n}})))\to\langle\overline{s_{0}}\rangle\Leftarrow\mathsf{f}_{n}^{-1}(t_{n},w_{n})\twoheadrightarrow\langle\overline{s_{n}}\rangle,\ldots,\mathsf{f}_{1}^{-1}(t_{1},w_{1})\twoheadrightarrow\langle\overline{s_{1}}\rangle in bsubscript𝑏{\mathcal{R}}_{b} and a substitution θ𝜃\theta such that 𝖿1(r,β(y¯,wn¯))θ=𝖿1(t,π^)superscript𝖿1𝑟𝛽¯𝑦¯subscript𝑤𝑛𝜃superscript𝖿1𝑡^𝜋\mathsf{f}^{-1}(r,\beta(\overline{y},\overline{w_{n}}))\theta=\mathsf{f}^{-1}(t,\widehat{\pi}), 𝖿i1(ti,wi)θbki𝖼si¯θsubscriptsuperscript𝖼subscriptsubscript𝑏subscript𝑘𝑖superscriptsubscript𝖿𝑖1subscript𝑡𝑖subscript𝑤𝑖𝜃delimited-⟨⟩¯subscript𝑠𝑖𝜃\mathsf{f}_{i}^{-1}(t_{i},w_{i})\theta\stackrel{{\scriptstyle\mathsf{c}}}{{\rightarrow}}_{{\mathcal{R}}_{b_{k_{i}}}}\langle\overline{s_{i}}\rangle\theta, i=n,,1𝑖𝑛1i=n,\ldots,1, and 𝖿1(r,ws)θ=s¯superscript𝖿1𝑟𝑤𝑠𝜃delimited-⟨⟩¯𝑠\mathsf{f}^{-1}(r,ws)\theta=\langle\overline{s}\rangle. Assume that σ𝜎\sigma is the restriction of θ𝜃\theta to the variables of the rule, excluding the fresh variables wn¯¯subscript𝑤𝑛\overline{w_{n}}, and that wiθ=π^isubscript𝑤𝑖𝜃subscript^𝜋𝑖w_{i}\theta=\widehat{\pi}_{i} for all i=1,,n𝑖1𝑛i=1,\ldots,n. Therefore, 𝖿1(r,β(y¯,wn¯))θ=𝖿1(rσ,β(y¯σ,π^1,,π^n)\mathsf{f}^{-1}(r,\beta(\overline{y},\overline{w_{n}}))\theta=\mathsf{f}^{-1}(r\sigma,\beta(\overline{y}\sigma,\widehat{\pi}_{1},\ldots,\widehat{\pi}_{n}), 𝖿i1(ti,wi)θ=𝖿i1(tiσ,π^i)superscriptsubscript𝖿𝑖1subscript𝑡𝑖subscript𝑤𝑖𝜃superscriptsubscript𝖿𝑖1subscript𝑡𝑖𝜎subscript^𝜋𝑖\mathsf{f}_{i}^{-1}(t_{i},w_{i})\theta=\mathsf{f}_{i}^{-1}(t_{i}\sigma,\widehat{\pi}_{i}) and si¯θ=si¯σdelimited-⟨⟩¯subscript𝑠𝑖𝜃delimited-⟨⟩¯subscript𝑠𝑖𝜎\langle\overline{s_{i}}\rangle\theta=\langle\overline{s_{i}}\sigma\rangle, i=1,,n𝑖1𝑛i=1,\ldots,n. Then, by definition of bkisubscriptsubscript𝑏subscript𝑘𝑖{\mathcal{R}}_{b_{k_{i}}}, we have that ki<ksubscript𝑘𝑖𝑘k_{i}<k for all i=1,,n𝑖1𝑛i=1,\ldots,n and, thus, by the induction hypothesis, we have tiσ,πi𝖼𝖿i(si¯σ),[]\langle t_{i}\sigma,\pi_{i}\rangle{\stackrel{{\scriptstyle\mathsf{c}}}{{\leftharpoondown}}\!\!{}_{{\mathcal{R}}}}\langle\mathsf{f}_{i}(\overline{s_{i}}\sigma),[\,]\rangle, i=1,,n𝑖1𝑛i=1,\ldots,n. Consider now the equivalent rule in {\mathcal{R}}: β:𝖿(s0¯)r𝖿1(s1¯)t1,,𝖿n(sn¯)tn\beta:\mathsf{f}(\overline{s_{0}})\to r\Leftarrow\mathsf{f}_{1}(\overline{s_{1}})\twoheadrightarrow t_{1},\ldots,\mathsf{f}_{n}(\overline{s_{n}})\twoheadrightarrow t_{n} in {\mathcal{R}}. Therefore, we have t,π𝖼𝖿(s¯),[]subscriptsuperscript𝖼𝑡𝜋𝖿¯𝑠\langle t,\pi\rangle\stackrel{{\scriptstyle\mathsf{c}}}{{\leftharpoondown}}_{\mathcal{R}}\langle\mathsf{f}(\overline{s}),[\,]\rangle,

σ=σ|`(𝒱𝖺𝗋(s0¯)\𝒱𝖺𝗋(r,s1¯,,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1¯,,sn¯)\sigma^{\prime}=\sigma\!\!\mathrel{|\grave{}}_{(\mathsf{{{\mathcal{V}}}ar}(\overline{s_{0}})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{1}},\ldots,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1}},\ldots,\overline{s_{n}})}

and π=β(σ,π1,,πn)𝜋𝛽superscript𝜎subscript𝜋1subscript𝜋𝑛\pi=\beta(\sigma^{\prime},\pi_{1},\ldots,\pi_{n}). Finally, since {y¯}=(𝒱𝖺𝗋(s0¯)\𝒱𝖺𝗋(r,s1¯,,sn¯,tn¯))i=1n𝒱𝖺𝗋(ti)\𝒱𝖺𝗋(r,si+1¯,,sn¯)¯𝑦\𝒱𝖺𝗋¯subscript𝑠0𝒱𝖺𝗋𝑟¯subscript𝑠1¯subscript𝑠𝑛¯subscript𝑡𝑛superscriptsubscript𝑖1𝑛\𝒱𝖺𝗋subscript𝑡𝑖𝒱𝖺𝗋𝑟¯subscript𝑠𝑖1¯subscript𝑠𝑛\{\overline{y}\}=(\mathsf{{{\mathcal{V}}}ar}(\overline{s_{0}})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{1}},\ldots,\overline{s_{n}},\overline{t_{n}}))\cup\bigcup_{i=1}^{n}\mathsf{{{\mathcal{V}}}ar}(t_{i})\backslash\mathsf{{{\mathcal{V}}}ar}(r,\overline{s_{i+1}},\ldots,\overline{s_{n}}), we can conclude that π^=π^𝜋𝜋\widehat{\pi}=\pi. \qed

5.3 Improving the transformation for injective functions

When a function is injective, one can expect the injectivization transformation to be unnecessary. This is not generally true, since some additional syntactic conditions might also be required. Furthermore, depending on the considered setting, it can be necessary to have an injective system, rather than an injective function. Consider, e.g., the following simple TRS:

={𝖿𝟣𝖿𝟤,𝖿𝟤𝟢,𝗀𝟣𝗀𝟤,𝗀𝟤𝟢}formulae-sequencesubscript𝖿1subscript𝖿2formulae-sequencesubscript𝖿20formulae-sequencesubscript𝗀1subscript𝗀2subscript𝗀20{\mathcal{R}}=\{~{}\mathsf{f_{1}}\to\mathsf{f_{2}},\mathsf{f_{2}}\to\mathsf{0},~{}\mathsf{g_{1}}\to\mathsf{g_{2}},~{}\mathsf{g_{2}}\to\mathsf{0}~{}\}

Here, all functions are clearly injective. However, given a reduction like 𝖿𝟣𝖿𝟤𝟢subscriptsubscript𝖿1subscript𝖿2subscript0\mathsf{f_{1}}\to_{\mathcal{R}}\mathsf{f_{2}}\to_{\mathcal{R}}\mathsf{0}, we do not know which rule should be applied to 𝟢0\mathsf{0} in order to go backwards until the initial term (actually, both the second and the fourth rules are applicable in the reverse direction).

Luckily, in our context, the injectivity of a function suffices since reductions in pcDCTRSs are performed in a single step. Therefore, given a reduction of the form 𝖿𝚒(sn¯)tsubscriptsuperscript𝖿𝚒¯subscript𝑠𝑛𝑡\mathsf{f}^{\mathtt{i}}(\overline{s_{n}})\to_{\mathcal{R}}t, a backward computation will have the form 𝖿1(t)sn¯subscriptsuperscript𝖿1𝑡delimited-⟨⟩¯subscript𝑠𝑛\mathsf{f}^{-1}(t)\to_{\mathcal{R}}\langle\overline{s_{n}}\rangle, so that we know that only the inverse rules of 𝖿𝖿\mathsf{f} are applicable.

Now, we present an improvement of the injectivization transformation presented in Section 5.1 which has some similarities with that in [24]. Here, we consider that the initial system is a TRS {\mathcal{R}} since, to the best of our knowledge, there is no reachability analysis defined for DCTRSs. In the following, given a term s𝑠s, we let

𝗋𝖺𝗇𝗀𝖾(s)={tsσt,σ:𝒱𝒯(𝒞),andt𝒯(𝒞)}𝗋𝖺𝗇𝗀𝖾𝑠conditional-set𝑡:superscriptsubscript𝑠𝜎𝑡𝜎formulae-sequencemaps-to𝒱𝒯𝒞and𝑡𝒯𝒞\mathsf{range}(s)=\{t\mid s\sigma\to_{\mathcal{R}}^{\ast}t,~{}\sigma:{\mathcal{V}}\mapsto{\mathcal{T}}({\mathcal{C}}),~{}\mbox{and}~{}t\in{\mathcal{T}}({\mathcal{C}})\}

i.e., 𝗋𝖺𝗇𝗀𝖾(s)𝗋𝖺𝗇𝗀𝖾𝑠\mathsf{range}(s) returns a set with the constructor normal forms of all possible ground constructor instances of s𝑠s. Although computing this set is generally undecidable, there are some overapproximations based on the use of tree automata (see, e.g., [15] and the most recent approach for innermost rewriting [16]). Let us consider that 𝗋𝖺𝗇𝗀𝖾α(s)superscript𝗋𝖺𝗇𝗀𝖾𝛼𝑠\mathsf{range}^{\alpha}(s) is such an approximation, with 𝗋𝖺𝗇𝗀𝖾α(s)𝗋𝖺𝗇𝗀𝖾(s)𝗋𝖺𝗇𝗀𝖾𝑠superscript𝗋𝖺𝗇𝗀𝖾𝛼𝑠\mathsf{range}^{\alpha}(s)\supseteq\mathsf{range}(s) for all terms s𝑠s. Here, we are interested in determining when the right-hand sides, r1subscript𝑟1r_{1} and r2subscript𝑟2r_{2}, of two rules do not overlap, i.e., 𝗋𝖺𝗇𝗀𝖾(r1)𝗋𝖺𝗇𝗀𝖾(r2)=𝗋𝖺𝗇𝗀𝖾subscript𝑟1𝗋𝖺𝗇𝗀𝖾subscript𝑟2\mathsf{range}(r_{1})\cap\mathsf{range}(r_{2})=\varnothing. For this purpose, we will check whether 𝗋𝖺𝗇𝗀𝖾α(r1)𝗋𝖺𝗇𝗀𝖾α(r2)=superscript𝗋𝖺𝗇𝗀𝖾𝛼subscript𝑟1superscript𝗋𝖺𝗇𝗀𝖾𝛼subscript𝑟2\mathsf{range}^{\alpha}(r_{1})\cap\mathsf{range}^{\alpha}(r_{2})=\varnothing. Since finite tree automata are closed under intersection and the emptiness of a finite tree automata is decidable, checking the emptiness of 𝗋𝖺𝗇𝗀𝖾α(r1)𝗋𝖺𝗇𝗀𝖾α(r2)superscript𝗋𝖺𝗇𝗀𝖾𝛼subscript𝑟1superscript𝗋𝖺𝗇𝗀𝖾𝛼subscript𝑟2\mathsf{range}^{\alpha}(r_{1})\cap\mathsf{range}^{\alpha}(r_{2}) is decidable and can be used to safely identify non-overlapping right-hand sides, i.e., if 𝗋𝖺𝗇𝗀𝖾α(r1)𝗋𝖺𝗇𝗀𝖾α(r2)=superscript𝗋𝖺𝗇𝗀𝖾𝛼subscript𝑟1superscript𝗋𝖺𝗇𝗀𝖾𝛼subscript𝑟2\mathsf{range}^{\alpha}(r_{1})\cap\mathsf{range}^{\alpha}(r_{2})=\varnothing, then r1subscript𝑟1r_{1} and r2subscript𝑟2r_{2} are definitely non-overlapping; otherwise, they may be overlapping or non-overlapping.

Now, we summarize our method to simplify some trace terms. Given a constructor TRS {\mathcal{R}} and a rule β:lr:𝛽𝑙𝑟\beta:l\to r\in{\mathcal{R}}, we check the following conditions:

  1. 1.

    the right-hand side r𝑟r of the rule does not overlap with the right-hand side of any other rule defining the same function;

  2. 2.

    the rule is non-erasing, i.e., 𝒱𝖺𝗋(l)=𝒱𝖺𝗋(r)𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟\mathsf{{{\mathcal{V}}}ar}(l)=\mathsf{{{\mathcal{V}}}ar}(r);

  3. 3.

    the right-hand side r𝑟r contains a single occurrence of a defined function symbol, say 𝖿𝒟𝖿𝒟\mathsf{f}\in{\mathcal{D}}.

If these conditions hold, then the rule has the form lr[𝖿(s¯)]p𝑙𝑟subscriptdelimited-[]𝖿¯𝑠𝑝l\to r[\mathsf{f}(\overline{s})]_{p} with l𝑙l and 𝖿(s¯)𝖿¯𝑠\mathsf{f}(\overline{s}) basic terms,141414Note that l𝑙l is a basic term since we initially consider a constructor TRS and, thus, all left-hand sides are basic terms by definition. and r[x]p𝑟subscriptdelimited-[]𝑥𝑝r[x]_{p} and s¯¯𝑠\overline{s} constructor terms, where x𝑥x is a fresh variable. In this case, we can safely produce the following injective version:151515Since lr𝑙𝑟l\to r is non-erasing, the pcDCTRS rule lr[x]p𝖿(s¯)x𝑙𝑟subscriptdelimited-[]𝑥𝑝𝖿¯𝑠𝑥l\to r[x]_{p}\Leftarrow\mathsf{f}(\overline{s})\twoheadrightarrow x is trivially non-erasing too (according to [32], i.e., (𝒱𝖺𝗋(l)\𝒱𝖺𝗋(r[x]p,𝖿(s¯),x))𝒱𝖺𝗋(x)\𝒱𝖺𝗋(r[x]p)=\𝒱𝖺𝗋𝑙𝒱𝖺𝗋𝑟subscriptdelimited-[]𝑥𝑝𝖿¯𝑠𝑥\𝒱𝖺𝗋𝑥𝒱𝖺𝗋𝑟subscriptdelimited-[]𝑥𝑝(\mathsf{{{\mathcal{V}}}ar}(l)\backslash\mathsf{{{\mathcal{V}}}ar}(r[x]_{p},\mathsf{f}(\overline{s}),x))\cup\mathsf{{{\mathcal{V}}}ar}(x)\backslash\mathsf{{{\mathcal{V}}}ar}(r[x]_{p})=\varnothing) and, thus, no binding should be stored during the injectivization process.

l𝚒r[x]p,w𝖿𝚒(s¯)x,wsuperscript𝑙𝚒𝑟subscriptdelimited-[]𝑥𝑝𝑤superscript𝖿𝚒¯𝑠𝑥𝑤l^{\mathtt{i}}\to\langle r[x]_{p},w\rangle\Leftarrow\mathsf{f}^{\mathtt{i}}(\overline{s})\twoheadrightarrow\langle x,w\rangle

instead of

l𝚒r[x]p,β(w)𝖿𝚒(s¯)x,wsuperscript𝑙𝚒𝑟subscriptdelimited-[]𝑥𝑝𝛽𝑤superscript𝖿𝚒¯𝑠𝑥𝑤l^{\mathtt{i}}\to\langle r[x]_{p},\beta(w)\rangle\Leftarrow\mathsf{f}^{\mathtt{i}}(\overline{s})\twoheadrightarrow\langle x,w\rangle

Let us illustrate this improved transformation with a couple of examples.

Example 37

Consider the following TRS:

={𝖿(𝗌(x))𝗀(x),𝖿(𝖼(x))𝗁(x),𝗀(x)𝗌(x),𝗁(x)𝖼(x)}formulae-sequence𝖿𝗌𝑥𝗀𝑥formulae-sequence𝖿𝖼𝑥𝗁𝑥formulae-sequence𝗀𝑥𝗌𝑥𝗁𝑥𝖼𝑥{\mathcal{R}}=\{~{}\mathsf{f}(\mathsf{s}(x))\to\mathsf{g}(x),~{}\mathsf{f}(\mathsf{c}(x))\to\mathsf{h}(x),~{}\mathsf{g}(x)\to\mathsf{s}(x),~{}\mathsf{h}(x)\to\mathsf{c}(x)\}

Here, it can easily be shown that 𝗋𝖺𝗇𝗀𝖾α(𝗀(x))𝗋𝖺𝗇𝗀𝖾α(𝗁(x))=superscript𝗋𝖺𝗇𝗀𝖾𝛼𝗀𝑥superscript𝗋𝖺𝗇𝗀𝖾𝛼𝗁𝑥\mathsf{range}^{\alpha}(\mathsf{g}(x))\cap\mathsf{range}^{\alpha}(\mathsf{h}(x))=\varnothing, the two rules defining 𝖿𝖿\mathsf{f} are non-erasing, and both contain a single occurrence of a defined function symbol in the righ-hand sides. Therefore, our improved injectivization applies and we get the following pcDCTRS fsubscript𝑓{\mathcal{R}}_{f}:

𝖿𝚒(𝗌(x))y,w𝗀𝚒(x)y,w𝗀𝚒(x)𝗌(x),β3𝖿𝚒(𝖼(x))y,w𝗁𝚒(x)y,w𝗁𝚒(x)𝖼(x),β4superscript𝖿𝚒𝗌𝑥𝑦𝑤superscript𝗀𝚒𝑥𝑦𝑤superscript𝗀𝚒𝑥𝗌𝑥subscript𝛽3superscript𝖿𝚒𝖼𝑥𝑦𝑤superscript𝗁𝚒𝑥𝑦𝑤superscript𝗁𝚒𝑥𝖼𝑥subscript𝛽4\begin{array}[]{r@{~}c@{~}l@{~~~~~~~~~}r@{~}c@{~}l}\mathsf{f}^{\mathtt{i}}(\mathsf{s}(x))~{}&\to\hfil~{}&\langle y,w\rangle\Leftarrow\mathsf{g}^{\mathtt{i}}(x)\twoheadrightarrow\langle y,w\rangle\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}&\mathsf{g}^{\mathtt{i}}(x)~{}&\to\hfil~{}&\langle\mathsf{s}(x),\beta_{3}\rangle\\ \mathsf{f}^{\mathtt{i}}(\mathsf{c}(x))~{}&\to\hfil~{}&\langle y,w\rangle\Leftarrow\mathsf{h}^{\mathtt{i}}(x)\twoheadrightarrow\langle y,w\rangle\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}&\mathsf{h}^{\mathtt{i}}(x)~{}&\to\hfil~{}&\langle\mathsf{c}(x),\beta_{4}\rangle\end{array}

In contrast, the original injectivization transformation would return the following system:

𝖿𝚒(𝗌(x))y,β1(w)𝗀𝚒(x)y,w𝗀𝚒(x)𝗌(x),β3𝖿𝚒(𝖼(x))y,β2(w)𝗁𝚒(x)y,w𝗁𝚒(x)𝖼(x),β4superscript𝖿𝚒𝗌𝑥𝑦subscript𝛽1𝑤superscript𝗀𝚒𝑥𝑦𝑤superscript𝗀𝚒𝑥𝗌𝑥subscript𝛽3superscript𝖿𝚒𝖼𝑥𝑦subscript𝛽2𝑤superscript𝗁𝚒𝑥𝑦𝑤superscript𝗁𝚒𝑥𝖼𝑥subscript𝛽4\begin{array}[]{r@{~}c@{~}l@{~~~~~~~~~}r@{~}c@{~}l}\mathsf{f}^{\mathtt{i}}(\mathsf{s}(x))~{}&\to\hfil~{}&\langle y,\beta_{1}(w)\rangle\Leftarrow\mathsf{g}^{\mathtt{i}}(x)\twoheadrightarrow\langle y,w\rangle\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}&\mathsf{g}^{\mathtt{i}}(x)~{}&\to\hfil~{}&\langle\mathsf{s}(x),\beta_{3}\rangle\\ \mathsf{f}^{\mathtt{i}}(\mathsf{c}(x))~{}&\to\hfil~{}&\langle y,\beta_{2}(w)\rangle\Leftarrow\mathsf{h}^{\mathtt{i}}(x)\twoheadrightarrow\langle y,w\rangle\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}&\mathsf{h}^{\mathtt{i}}(x)~{}&\to\hfil~{}&\langle\mathsf{c}(x),\beta_{4}\rangle\end{array}

Finally, the inverse system bsubscript𝑏{\mathcal{R}}_{b} obtained from fsubscript𝑓{\mathcal{R}}_{f} using the original transformation has the following form:

𝖿1(y,w)𝗌(x)𝗀1(y,w)x𝗀1(𝗌(x),β3)x𝖿1(y,w)𝖼(x)𝗁1(y,w)x𝗁1(𝖼(x),β4)xsuperscript𝖿1𝑦𝑤delimited-⟨⟩𝗌𝑥superscript𝗀1𝑦𝑤delimited-⟨⟩𝑥superscript𝗀1𝗌𝑥subscript𝛽3delimited-⟨⟩𝑥superscript𝖿1𝑦𝑤delimited-⟨⟩𝖼𝑥superscript𝗁1𝑦𝑤delimited-⟨⟩𝑥superscript𝗁1𝖼𝑥subscript𝛽4delimited-⟨⟩𝑥\begin{array}[]{r@{~}c@{~}l@{~~~~~~~~~}r@{~}c@{~}l}\mathsf{f}^{-1}(y,w)~{}&\to\hfil~{}&\langle\mathsf{s}(x)\rangle\Leftarrow\mathsf{g}^{-1}(y,w)\twoheadrightarrow\langle x\rangle\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}&\mathsf{g}^{-1}(\mathsf{s}(x),\beta_{3})~{}&\to\hfil~{}&\langle x\rangle\\ \mathsf{f}^{-1}(y,w)~{}&\to\hfil~{}&\langle\mathsf{c}(x)\rangle\Leftarrow\mathsf{h}^{-1}(y,w)\twoheadrightarrow\langle x\rangle\hfil~{}~{}~{}~{}~{}~{}~{}~{}~{}&\mathsf{h}^{-1}(\mathsf{c}(x),\beta_{4})~{}&\to\hfil~{}&\langle x\rangle\end{array}

For instance, given the forward reduction 𝖿𝚒(𝗌(𝟢))f𝗌(𝟢),β3subscriptsubscript𝑓superscript𝖿𝚒𝗌0𝗌0subscript𝛽3\mathsf{f}^{\mathtt{i}}(\mathsf{s}(\mathsf{0}))\to_{{\mathcal{R}}_{f}}\langle\mathsf{s}(\mathsf{0}),\beta_{3}\rangle, we can build the corresponding backward reduction: 𝖿1(𝗌(𝟢),β3)b𝗌(𝟢)subscriptsubscript𝑏superscript𝖿1𝗌0subscript𝛽3delimited-⟨⟩𝗌0\mathsf{f}^{-1}(\mathsf{s}(\mathsf{0}),\beta_{3})\to_{{\mathcal{R}}_{b}}\langle\mathsf{s}(\mathsf{0})\rangle.

Note, however, that the left-hand sides of 𝖿1superscript𝖿1\mathsf{f}^{-1} overlap and we should reduce the conditions in order to determine which rule to apply. Therefore, in some cases, there is a trade-off between the size of the trace terms and the complexity of the reduction steps.

The example above, though, only produces a rather limited improvement since the considered functions are not recursive. Our next example shows a much significant improvement. Here, we consider the function 𝗓𝗂𝗉𝗓𝗂𝗉\mathsf{zip} (also used in [24] to illustrate the benefits of an injectivity analysis).

Example 38

Consider the following TRS {\mathcal{R}} defining the function 𝗓𝗂𝗉𝗓𝗂𝗉\mathsf{zip}:

𝗓𝗂𝗉([],ys)[]𝗓𝗂𝗉(xs,[])[]𝗓𝗂𝗉(x:xs,y:ys)𝗉𝖺𝗂𝗋(x,y):𝗓𝗂𝗉(xs,ys)\begin{array}[]{r@{~}c@{~}l}\mathsf{zip}([\,],ys)~{}&\to\hfil~{}&[\,]\\ \mathsf{zip}(xs,[\,])~{}&\to\hfil~{}&[\,]\\ \mathsf{zip}(x:xs,y:ys)~{}&\to\hfil~{}&\mathsf{pair}(x,y):\mathsf{zip}(xs,ys)\\ \end{array}

Here, since the third rule is non-erasing, its right-hand side contains a single occurrence of a defined function, 𝗓𝗂𝗉𝗓𝗂𝗉\mathsf{zip}, and it does not overlap with any other right-hand side, our improved injectivization applies and we get the following pcDCTRS fsubscript𝑓{\mathcal{R}}_{f}:

𝗓𝗂𝗉𝚒([],ys)[],β1(ys)𝗓𝗂𝗉𝚒(xs,[])[],β2(xs)𝗓𝗂𝗉𝚒(x:xs,y:ys)𝗉𝖺𝗂𝗋(x,y):zs,w𝗓𝗂𝗉𝚒(xs,ys)zs,w\begin{array}[]{r@{~}c@{~}l}\mathsf{zip}^{\mathtt{i}}([\,],ys)~{}&\to\hfil~{}&\langle[\,],\beta_{1}(ys)\rangle\\ \mathsf{zip}^{\mathtt{i}}(xs,[\,])~{}&\to\hfil~{}&\langle[\,],\beta_{2}(xs)\rangle\\ \mathsf{zip}^{\mathtt{i}}(x:xs,y:ys)~{}&\to\hfil~{}&\langle\mathsf{pair}(x,y):zs,w\rangle\Leftarrow\mathsf{zip}^{\mathtt{i}}(xs,ys)\twoheadrightarrow\langle zs,w\rangle\\ \end{array}

In contrast, the original injectivization transformation would return the following system fsubscriptsuperscript𝑓{\mathcal{R}}^{\prime}_{f}:

𝗓𝗂𝗉𝚒([],ys)[],β1(ys)𝗓𝗂𝗉𝚒(xs,[])[],β2(xs)𝗓𝗂𝗉𝚒(x:xs,y:ys)𝗉𝖺𝗂𝗋(x,y):zs,β3(w)𝗓𝗂𝗉𝚒(xs,ys)zs,w\begin{array}[]{r@{~}c@{~}l}\mathsf{zip}^{\mathtt{i}}([\,],ys)~{}&\to\hfil~{}&\langle[\,],\beta_{1}(ys)\rangle\\ \mathsf{zip}^{\mathtt{i}}(xs,[\,])~{}&\to\hfil~{}&\langle[\,],\beta_{2}(xs)\rangle\\ \mathsf{zip}^{\mathtt{i}}(x:xs,y:ys)~{}&\to\hfil~{}&\langle\mathsf{pair}(x,y):zs,\beta_{3}(w)\rangle\Leftarrow\mathsf{zip}^{\mathtt{i}}(xs,ys)\twoheadrightarrow\langle zs,w\rangle\\ \end{array}

It might seem a small difference, but if we call 𝗓𝗂𝗉𝚒superscript𝗓𝗂𝗉𝚒\mathsf{zip}^{\mathtt{i}} with two lists of n𝑛n elements, the system fsubscriptsuperscript𝑓{\mathcal{R}}^{\prime}_{f} would build a trace term of the form β3(β3(β1()))subscript𝛽3subscript𝛽3subscript𝛽1\beta_{3}(\ldots\beta_{3}(\beta_{1}(\ldots))\ldots) with n𝑛n nested constructors β3subscript𝛽3\beta_{3}, while fsubscript𝑓{\mathcal{R}}_{f} would just build the trace term β1()subscript𝛽1\beta_{1}(\ldots). For large values of n𝑛n, this is a significant improvement in memory usage.

6 Bidirectional Program Transformation

We illustrate a practical application of our reversibilization technique in the context of bidirectional program transformation (see [10] for a survey). In particular, we consider the so-called view-update problem. Here, we have a data structure (e.g., a database) called the source, which is transformed to another data structure, called the view. Typically, we have a view function, 𝗏𝗂𝖾𝗐:𝑆𝑜𝑢𝑟𝑐𝑒𝑉𝑖𝑒𝑤:𝗏𝗂𝖾𝗐𝑆𝑜𝑢𝑟𝑐𝑒𝑉𝑖𝑒𝑤\mathsf{view}\!\!:\mathit{Source}\to\mathit{View} that takes the source and returns the corresponding view, together with an update function, 𝗎𝗉𝖽:𝑉𝑖𝑒𝑤×𝑆𝑜𝑢𝑟𝑐𝑒𝑆𝑜𝑢𝑟𝑐𝑒:𝗎𝗉𝖽𝑉𝑖𝑒𝑤𝑆𝑜𝑢𝑟𝑐𝑒𝑆𝑜𝑢𝑟𝑐𝑒\mathsf{upd}\!\!:\mathit{View}\times\mathit{Source}\to\mathit{Source} that propagates the changes in a modified view to the original source. Two basic properties that these functions should satisfy in order to be well-behaved are the following [13]:

s𝑆𝑜𝑢𝑟𝑐𝑒,v𝑉𝑖𝑒𝑤:𝗏𝗂𝖾𝗐(𝗎𝗉𝖽(v,s))=vs𝑆𝑜𝑢𝑟𝑐𝑒:𝗎𝗉𝖽(𝗏𝗂𝖾𝗐(s),s)=sformulae-sequencefor-all𝑠𝑆𝑜𝑢𝑟𝑐𝑒for-all𝑣𝑉𝑖𝑒𝑤:𝗏𝗂𝖾𝗐𝗎𝗉𝖽𝑣𝑠𝑣for-all𝑠𝑆𝑜𝑢𝑟𝑐𝑒:𝗎𝗉𝖽𝗏𝗂𝖾𝗐𝑠𝑠𝑠\begin{array}[]{r@{}cl}\forall s\in\mathit{Source},\forall v\in\mathit{View}&:&\mathsf{view}(\mathsf{upd}(v,s))=v\\ \forall s\in\mathit{Source}&:&\mathsf{upd}(\mathsf{view}(s),s)=s\end{array}

Bidirectionalization (first proposed in the database community [5]) basically consists in, given a view function, “bidirectionalize” it in order to derive an appropriate update function. For this purpose, first, a view complement function is usually defined, say 𝗏𝗂𝖾𝗐csuperscript𝗏𝗂𝖾𝗐𝑐\mathsf{view}^{c}, so that the tupled function

𝗏𝗂𝖾𝗐𝗏𝗂𝖾𝗐c:𝑆𝑜𝑢𝑟𝑐𝑒𝑉𝑖𝑒𝑤×𝐶𝑜𝑚𝑝:𝗏𝗂𝖾𝗐superscript𝗏𝗂𝖾𝗐𝑐𝑆𝑜𝑢𝑟𝑐𝑒𝑉𝑖𝑒𝑤𝐶𝑜𝑚𝑝\mathsf{view}\vartriangle\mathsf{view}^{c}\!\!:\mathit{Source}\to\mathit{View}\times\mathit{Comp}

becomes injective. Therefore, the update function can be defined as follows:

𝗎𝗉𝖽(v,s)=(𝗏𝗂𝖾𝗐𝗏𝗂𝖾𝗐c)1(v,𝗏𝗂𝖾𝗐c(s))𝗎𝗉𝖽𝑣𝑠superscript𝗏𝗂𝖾𝗐superscript𝗏𝗂𝖾𝗐𝑐1𝑣superscript𝗏𝗂𝖾𝗐𝑐𝑠\mathsf{upd}(v,s)=(\mathsf{view}\vartriangle\mathsf{view}^{c})^{-1}(v,\mathsf{view}^{c}(s))

This approach has been applied to bidirectionalize view functions in a functional language in [24].

In the following, we apply our injectivization and inversion transformations in order to produce a bidirectionalization transformation that may be useful in the context of the view-update problem (with some limitations). Let us assume that we have a view function, 𝗏𝗂𝖾𝗐𝗏𝗂𝖾𝗐\mathsf{view}, that takes a source and returns the corresponding view, and which is defined by means of a pcDCTRS. Following our approach, given the original program {\mathcal{R}}, we produce an injectivized version fsubscript𝑓{\mathcal{R}}_{f} and the corresponding inverse bsubscript𝑏{\mathcal{R}}_{b}. Therefore, in principle, one can use fbsubscript𝑓subscript𝑏{\mathcal{R}}_{f}\cup{\mathcal{R}}_{b}, which will include the functions 𝗏𝗂𝖾𝗐𝚒superscript𝗏𝗂𝖾𝗐𝚒\mathsf{view}^{\mathtt{i}} and 𝗏𝗂𝖾𝗐1superscript𝗏𝗂𝖾𝗐1\mathsf{view}^{-1}\!, to define an update function as follows:

𝗎𝗉𝖽(v,s)s𝗏𝗂𝖾𝗐𝚒(s)v,π,𝗏𝗂𝖾𝗐1(v,π)sformulae-sequence𝗎𝗉𝖽𝑣𝑠superscript𝑠superscript𝗏𝗂𝖾𝗐𝚒𝑠superscript𝑣𝜋superscript𝗏𝗂𝖾𝗐1𝑣𝜋delimited-⟨⟩superscript𝑠\mathsf{upd}(v,s)\to s^{\prime}\Leftarrow\mathsf{view}^{\mathtt{i}}(s)\twoheadrightarrow\langle v^{\prime},\pi\rangle,\mathsf{view}^{-1}\!(v,\pi)\twoheadrightarrow\langle s^{\prime}\rangle

where s𝑠s is the original source, v𝑣v is the updated view, and ssuperscript𝑠s^{\prime}, the returned value, is the corresponding updated source. Note that, in our context, the function 𝗏𝗂𝖾𝗐𝚒superscript𝗏𝗂𝖾𝗐𝚒\mathsf{view}^{\mathtt{i}} is somehow equivalent to 𝗏𝗂𝖾𝗐𝗏𝗂𝖾𝗐c𝗏𝗂𝖾𝗐superscript𝗏𝗂𝖾𝗐𝑐\mathsf{view}\vartriangle\mathsf{view}^{c} above.

Let us now illustrate the bidirectionalization process with an example. Consider a particular data structure, a list of records of the form 𝗋(t,v)𝗋𝑡𝑣\mathsf{r}(t,v) where t𝑡t is the type of the record (e.g., 𝖻𝗈𝗈𝗄𝖻𝗈𝗈𝗄\mathsf{book}, 𝖽𝗏𝖽𝖽𝗏𝖽\mathsf{dvd}, 𝗉𝖾𝗇𝗉𝖾𝗇\mathsf{pen}, etc.) and v𝑣v is its price tag. The following system defines a view function that takes a type and a list of records, and returns a list with the price tags of the records of the given type:161616For simplicity, we restrict the record types to only 𝖻𝗈𝗈𝗄𝖻𝗈𝗈𝗄\mathsf{book} and 𝖽𝗏𝖽𝖽𝗏𝖽\mathsf{dvd}.

𝗏𝗂𝖾𝗐(t,𝗇𝗂𝗅)𝗇𝗂𝗅𝗏𝗂𝖾𝗐(t,𝗋(t,v):rs)𝗏𝖺𝗅(𝗋(t,v)):𝗏𝗂𝖾𝗐(t,rs)𝖾𝗊(t,t)𝗍𝗋𝗎𝖾𝗏𝗂𝖾𝗐(t,𝗋(t,v):rs)𝗏𝗂𝖾𝗐(t,rs)𝖾𝗊(t,t)𝖿𝖺𝗅𝗌𝖾𝖾𝗊(𝖻𝗈𝗈𝗄,𝖻𝗈𝗈𝗄)𝗍𝗋𝗎𝖾𝖾𝗊(𝖽𝗏𝖽,𝖽𝗏𝖽)𝗍𝗋𝗎𝖾𝖾𝗊(𝖻𝗈𝗈𝗄,𝖽𝗏𝖽)𝖿𝖺𝗅𝗌𝖾𝖾𝗊(𝖽𝗏𝖽,𝖻𝗈𝗈𝗄)𝖿𝖺𝗅𝗌𝖾𝗏𝖺𝗅(𝗋(t,v))v\begin{array}[]{rcl}\mathsf{view}(t,\mathsf{nil})&\to&\mathsf{nil}\\ \mathsf{view}(t,\mathsf{r}(t^{\prime},v):rs)&\to&\mathsf{val}(\mathsf{r}(t^{\prime},v)):\mathsf{view}(t,rs)\Leftarrow\mathsf{eq}(t,t^{\prime})\twoheadrightarrow\mathsf{true}\\ \mathsf{view}(t,\mathsf{r}(t^{\prime},v):rs)&\to&\mathsf{view}(t,rs)\Leftarrow\mathsf{eq}(t,t^{\prime})\twoheadrightarrow\mathsf{false}\\ \mathsf{eq}(\mathsf{book},\mathsf{book})&\to&\mathsf{true}\hskip 43.05542pt\mathsf{eq}(\mathsf{dvd},\mathsf{dvd})~{}\to~{}\mathsf{true}\\ \mathsf{eq}(\mathsf{book},\mathsf{dvd})&\to&\mathsf{false}\hskip 41.33322pt\mathsf{eq}(\mathsf{dvd},\mathsf{book})~{}\to~{}\mathsf{false}\\ \mathsf{val}(\mathsf{r}(t,v))&\to&v\\ \end{array}

However, this system is not a pcDCTRS. Here, we use a flattening transformation to produce the following (labeled) pcDCTRS {\mathcal{R}} which is equivalent for constructor derivations:

β1:𝗏𝗂𝖾𝗐(t,𝗇𝗂𝗅)𝗇𝗂𝗅β2:𝗏𝗂𝖾𝗐(t,𝗋(t,v):rs)p:r𝖾𝗊(t,t)𝗍𝗋𝗎𝖾,𝗏𝖺𝗅(𝗋(t,v))p,𝗏𝗂𝖾𝗐(t,rs)rβ3:𝗏𝗂𝖾𝗐(t,𝗋(t,v):rs)r𝖾𝗊(t,t)𝖿𝖺𝗅𝗌𝖾,𝗏𝗂𝖾𝗐(t,rs)rβ4:𝖾𝗊(𝖻𝗈𝗈𝗄,𝖻𝗈𝗈𝗄)𝗍𝗋𝗎𝖾β5:𝖾𝗊(𝖽𝗏𝖽,𝖽𝗏𝖽)𝗍𝗋𝗎𝖾β6:𝖾𝗊(𝖻𝗈𝗈𝗄,𝖽𝗏𝖽)𝖿𝖺𝗅𝗌𝖾β7:𝖾𝗊(𝖽𝗏𝖽,𝖻𝗈𝗈𝗄)𝖿𝖺𝗅𝗌𝖾β8:𝗏𝖺𝗅(𝗋(t,v))v\begin{array}[]{lr@{~}c@{~}l}\beta_{1}:&\mathsf{view}(t,\mathsf{nil})~{}&\to\hfil~{}&\mathsf{nil}\\ \beta_{2}:&\mathsf{view}(t,\mathsf{r}(t^{\prime},v):rs)~{}&\to\hfil~{}&p:r\\ &~{}&\hfil~{}&\mbox{}\Leftarrow\mathsf{eq}(t,t^{\prime})\twoheadrightarrow\mathsf{true},\mathsf{val}(\mathsf{r}(t^{\prime},v))\twoheadrightarrow p,\mathsf{view}(t,rs)\twoheadrightarrow r\\ \beta_{3}:&\mathsf{view}(t,\mathsf{r}(t^{\prime},v):rs)~{}&\to\hfil~{}&r\Leftarrow\mathsf{eq}(t,t^{\prime})\twoheadrightarrow\mathsf{false},\mathsf{view}(t,rs)\twoheadrightarrow r\\[4.30554pt] \beta_{4}:&\mathsf{eq}(\mathsf{book},\mathsf{book})~{}&\to\hfil~{}&\mathsf{true}\hskip 43.05542pt\beta_{5}:~{}\mathsf{eq}(\mathsf{dvd},\mathsf{dvd})~{}\to~{}\mathsf{true}\\ \beta_{6}:&\mathsf{eq}(\mathsf{book},\mathsf{dvd})~{}&\to\hfil~{}&\mathsf{false}\hskip 40.90265pt\beta_{7}:~{}\mathsf{eq}(\mathsf{dvd},\mathsf{book})~{}\to~{}\mathsf{false}\\[4.30554pt] \beta_{8}:&\mathsf{val}(\mathsf{r}(t,v))~{}&\to\hfil~{}&v\\ \end{array}

Now, we can apply our injectivization transformation which returns the following pcDCTRS f=𝐈()subscript𝑓𝐈{\mathcal{R}}_{f}=\mathbf{I}({\mathcal{R}}):

𝗏𝗂𝖾𝗐𝚒(t,𝗇𝗂𝗅)𝗇𝗂𝗅,β1(t)𝗏𝗂𝖾𝗐𝚒(t,𝗋(t,v):rs)p:r,β2(w1,w2,w3)𝖾𝗊𝚒(t,t)𝗍𝗋𝗎𝖾,w1,𝗏𝖺𝗅𝚒(𝗋(t,v))p,w2,𝗏𝗂𝖾𝗐𝚒(t,rs)r,w3𝗏𝗂𝖾𝗐𝚒(t,𝗋(t,v):rs)r,β3(v,w1,w2)𝖾𝗊𝚒(t,t)𝖿𝖺𝗅𝗌𝖾,w1,𝗏𝗂𝖾𝗐𝚒(t,rs)r,w2𝖾𝗊𝚒(𝖻𝗈𝗈𝗄,𝖻𝗈𝗈𝗄)𝗍𝗋𝗎𝖾,β4𝖾𝗊𝚒(𝖽𝗏𝖽,𝖽𝗏𝖽)𝗍𝗋𝗎𝖾,β5𝖾𝗊𝚒(𝖻𝗈𝗈𝗄,𝖽𝗏𝖽)𝖿𝖺𝗅𝗌𝖾,β6𝖾𝗊𝚒(𝖽𝗏𝖽,𝖻𝗈𝗈𝗄)𝖿𝖺𝗅𝗌𝖾,β7𝗏𝖺𝗅𝚒(𝗋(t,v))v,β8(t)\begin{array}[]{@{}r@{~}c@{~}l@{}}\mathsf{view}^{\mathtt{i}}(t,\mathsf{nil})~{}&\to\hfil~{}&\langle\mathsf{nil},\beta_{1}(t)\rangle\\ \mathsf{view}^{\mathtt{i}}(t,\mathsf{r}(t^{\prime},v):rs)~{}&\to\hfil~{}&\langle p:r,\beta_{2}(w_{1},w_{2},w_{3})\rangle\\ ~{}&\hfil~{}&\mbox{}\Leftarrow\mathsf{eq}^{\mathtt{i}}(t,t^{\prime})\twoheadrightarrow\langle\mathsf{true},w_{1}\rangle,\mathsf{val}^{\mathtt{i}}(\mathsf{r}(t^{\prime},v))\twoheadrightarrow\langle p,w_{2}\rangle,\mathsf{view}^{\mathtt{i}}(t,rs)\twoheadrightarrow\langle r,w_{3}\rangle\\ \mathsf{view}^{\mathtt{i}}(t,\mathsf{r}(t^{\prime},v):rs)~{}&\to\hfil~{}&\langle r,\beta_{3}(v,w_{1},w_{2})\rangle\\ ~{}&\hfil~{}&\mbox{}\Leftarrow\mathsf{eq}^{\mathtt{i}}(t,t^{\prime})\twoheadrightarrow\langle\mathsf{false},w_{1}\rangle,\mathsf{view}^{\mathtt{i}}(t,rs)\twoheadrightarrow\langle r,w_{2}\rangle\\[4.30554pt] \mathsf{eq}^{\mathtt{i}}(\mathsf{book},\mathsf{book})~{}&\to\hfil~{}&\langle\mathsf{true},\beta_{4}\rangle\hskip 47.36096pt\mathsf{eq}^{\mathtt{i}}(\mathsf{dvd},\mathsf{dvd})\>\to\>\langle\mathsf{true},\beta_{5}\rangle\\ \mathsf{eq}^{\mathtt{i}}(\mathsf{book},\mathsf{dvd})~{}&\to\hfil~{}&\langle\mathsf{false},\beta_{6}\rangle\hskip 40.90265pt\mathsf{eq}^{\mathtt{i}}(\mathsf{dvd},\mathsf{book})\>\to\>\langle\mathsf{false},\beta_{7}\rangle\\[4.30554pt] \mathsf{val}^{\mathtt{i}}(\mathsf{r}(t,v))~{}&\to\hfil~{}&\langle v,\beta_{8}(t)\rangle\\ \end{array}

Finally, inversion returns the following pcDCTRS b=𝐈(f)subscript𝑏𝐈subscript𝑓{\mathcal{R}}_{b}=\mathbf{I}({\mathcal{R}}_{f}):

𝗏𝗂𝖾𝗐1(𝗇𝗂𝗅,β1(t))t,𝗇𝗂𝗅𝗏𝗂𝖾𝗐1(p:r,β2(w1,w2,w3))t,𝗋(t,v):rs𝖾𝗊1(𝗍𝗋𝗎𝖾,w1)t,t,𝗏𝖺𝗅1(p,w2)𝗋(t,v),𝗏𝗂𝖾𝗐1(r,w3)t,rs𝗏𝗂𝖾𝗐1(r,β3(v,w1,w2))t,𝗋(t,v):rs𝖾𝗊1(𝖿𝖺𝗅𝗌𝖾,w1)t,t,𝗏𝗂𝖾𝗐1(r,w2)t,rs𝖾𝗊1(𝗍𝗋𝗎𝖾,β4)𝖻𝗈𝗈𝗄,𝖻𝗈𝗈𝗄𝖾𝗊1(𝗍𝗋𝗎𝖾,β5)𝖽𝗏𝖽,𝖽𝗏𝖽𝖾𝗊1(𝖿𝖺𝗅𝗌𝖾,β6)𝖻𝗈𝗈𝗄,𝖽𝗏𝖽𝖾𝗊1(𝖿𝖺𝗅𝗌𝖾,β7)𝖽𝗏𝖽,𝖻𝗈𝗈𝗄𝗏𝖺𝗅1(v,β8(t))𝗋(t,v)\begin{array}[]{@{}r@{~}c@{~}l@{}}\mathsf{view}^{-1}\!(\mathsf{nil},\beta_{1}(t))~{}&\to\hfil~{}&\langle t,\mathsf{nil}\rangle\\ \mathsf{view}^{-1}\!(p:r,\beta_{2}(w_{1},w_{2},w_{3}))~{}&\to\hfil~{}&\langle t,\mathsf{r}(t^{\prime},v):rs\rangle\\ ~{}&\hfil~{}&\mbox{}\Leftarrow\mathsf{eq}^{-1}\!(\mathsf{true},w_{1})\twoheadrightarrow\langle t,t^{\prime}\rangle,\mathsf{val}^{-1}\!(p,w_{2})\twoheadrightarrow\langle\mathsf{r}(t^{\prime},v)\rangle,\mathsf{view}^{-1}\!(r,w_{3})\twoheadrightarrow\langle t,rs\rangle\\ \mathsf{view}^{-1}\!(r,\beta_{3}(v,w_{1},w_{2}))~{}&\to\hfil~{}&\langle t,\mathsf{r}(t^{\prime},v):rs\rangle\\ ~{}&\hfil~{}&\mbox{}\Leftarrow\mathsf{eq}^{-1}\!(\mathsf{false},w_{1})\twoheadrightarrow\langle t,t^{\prime}\rangle,\mathsf{view}^{-1}\!(r,w_{2})\twoheadrightarrow\langle t,rs\rangle\\[4.30554pt] \mathsf{eq}^{-1}\!(\mathsf{true},\beta_{4})~{}&\to\hfil~{}&\langle\mathsf{book},\mathsf{book}\rangle\hskip 10.76385pt\mathsf{eq}^{-1}\!(\mathsf{true},\beta_{5})\>\to\>\langle\mathsf{dvd},\mathsf{dvd}\rangle\\ \mathsf{eq}^{-1}(\mathsf{false},\beta_{6})~{}&\to\hfil~{}&\langle\mathsf{book},\mathsf{dvd}\rangle\hskip 15.0694pt\mathsf{eq}^{-1}\!(\mathsf{false},\beta_{7})\>\to\>\langle\mathsf{dvd},\mathsf{book}\rangle\\[4.30554pt] \mathsf{val}^{-1}\!(v,\beta_{8}(t))~{}&\to\hfil~{}&\langle\mathsf{r}(t,v)\rangle\\ \end{array}

For instance, the term 𝗏𝗂𝖾𝗐(𝖻𝗈𝗈𝗄,[𝗋(𝖻𝗈𝗈𝗄,𝟣𝟤),𝗋(𝖽𝗏𝖽,𝟤𝟦)])𝗏𝗂𝖾𝗐𝖻𝗈𝗈𝗄𝗋𝖻𝗈𝗈𝗄12𝗋𝖽𝗏𝖽24\mathsf{view}(\mathsf{book},[\mathsf{r}(\mathsf{book},\mathsf{12}),\mathsf{r}(\mathsf{dvd},\mathsf{24})]), reduces to [𝟣𝟤]delimited-[]12[\mathsf{12}] in the original system {\mathcal{R}}. Given a modified view, e.g., [𝟣𝟧]delimited-[]15[\mathsf{15}], we can compute the modified source using function 𝗎𝗉𝖽𝗎𝗉𝖽\mathsf{upd} above:

𝗎𝗉𝖽([𝗋(𝖻𝗈𝗈𝗄,𝟣𝟤),𝗋(𝖽𝗏𝖽,𝟤𝟦)],[𝟣𝟧])𝗎𝗉𝖽𝗋𝖻𝗈𝗈𝗄12𝗋𝖽𝗏𝖽24delimited-[]15\mathsf{upd}([\mathsf{r}(\mathsf{book},\mathsf{12}),\mathsf{r}(\mathsf{dvd},\mathsf{24})],~{}[\mathsf{15}])

Here, we have the following subcomputations:171717Note that, in this case, the function 𝗏𝗂𝖾𝗐𝗏𝗂𝖾𝗐\mathsf{view} requires not only the source but also the additional parameter 𝖻𝗈𝗈𝗄𝖻𝗈𝗈𝗄\mathsf{book}.

𝗏𝗂𝖾𝗐𝚒(𝖻𝗈𝗈𝗄,[𝗋(𝖻𝗈𝗈𝗄,𝟣𝟤),𝗋(𝖽𝗏𝖽,𝟤𝟦)])f[𝟣𝟤],β2(β4,β8(𝖻𝗈𝗈𝗄),β3(𝟤𝟦,β6,β1(𝖻𝗈𝗈𝗄)))𝗏𝗂𝖾𝗐1([𝟣𝟧],β2(β4,β8(𝖻𝗈𝗈𝗄),β3(𝟤𝟦,β6,β1(𝖻𝗈𝗈𝗄))))b𝖻𝗈𝗈𝗄,[𝗋(𝖻𝗈𝗈𝗄,𝟣𝟧),𝗋(𝖽𝗏𝖽,𝟤𝟦)]superscript𝗏𝗂𝖾𝗐𝚒𝖻𝗈𝗈𝗄𝗋𝖻𝗈𝗈𝗄12𝗋𝖽𝗏𝖽24subscriptsubscript𝑓absentdelimited-[]12subscript𝛽2subscript𝛽4subscript𝛽8𝖻𝗈𝗈𝗄subscript𝛽324subscript𝛽6subscript𝛽1𝖻𝗈𝗈𝗄superscript𝗏𝗂𝖾𝗐1delimited-[]15subscript𝛽2subscript𝛽4subscript𝛽8𝖻𝗈𝗈𝗄subscript𝛽324subscript𝛽6subscript𝛽1𝖻𝗈𝗈𝗄subscriptsubscript𝑏absent𝖻𝗈𝗈𝗄𝗋𝖻𝗈𝗈𝗄15𝗋𝖽𝗏𝖽24\begin{array}[]{l}\mathsf{view}^{\mathtt{i}}(\mathsf{book},[\mathsf{r}(\mathsf{book},\mathsf{12}),\mathsf{r}(\mathsf{dvd},\mathsf{24})])\\ \hskip 86.11084pt\to_{{\mathcal{R}}_{f}}\langle[\mathsf{12}],\beta_{2}(\beta_{4},\beta_{8}(\mathsf{book}),\beta_{3}(\mathsf{24},\beta_{6},\beta_{1}(\mathsf{book})))\rangle\\ \mathsf{view}^{-1}\!([\mathsf{15}],\beta_{2}(\beta_{4},\beta_{8}(\mathsf{book}),\beta_{3}(\mathsf{24},\beta_{6},\beta_{1}(\mathsf{book}))))\\ \hskip 86.11084pt\to_{{\mathcal{R}}_{b}}\langle\mathsf{book},[\mathsf{r}(\mathsf{book},\mathsf{15}),\mathsf{r}(\mathsf{dvd},\mathsf{24})]\rangle\end{array}

Thus 𝗎𝗉𝖽𝗎𝗉𝖽\mathsf{upd} returns the updated source [𝗋(𝖻𝗈𝗈𝗄,𝟣𝟧),𝗋(𝖽𝗏𝖽,𝟤𝟦)]𝗋𝖻𝗈𝗈𝗄15𝗋𝖽𝗏𝖽24[\mathsf{r}(\mathsf{book},\mathsf{15}),\mathsf{r}(\mathsf{dvd},\mathsf{24})], as expected. We note that the considered example cannot be transformed using the technique in [24], the closer to our approach, since the right-hand sides of some rules contain functions which are not treeless.181818A call is treeless if it has the form 𝖿(x1,,xn)𝖿subscript𝑥1subscript𝑥𝑛\mathsf{f}(x_{1},\ldots,x_{n}) and x1,,xnsubscript𝑥1subscript𝑥𝑛x_{1},\ldots,x_{n} are different variables. Nevertheless, one could consider a transformation from pcDCTRS to functional programs with treeless functions so that the technique in [24] becomes applicable.

Our approach can solve a view-update problem as long as the view function can be encoded in a pcDCTRS. When this is the case, the results from Section 5 guarantee that function 𝗎𝗉𝖽𝗎𝗉𝖽\mathsf{upd} is well defined. Formally analyzing the class of view functions that can be represented with a pcDCTRS is an interesting topic for further research.

7 Related Work

There is no widely accepted notion of reversible computing. In this work, we have considered one of its most popular definitions, according to which a computation principle is reversible if there is a method to undo a (forward) computation. Moreover, we expect to get back to an exact past state of the computation. This is often referred to as full reversibility.

As we have mentioned in the introduction, some of the most promising applications of reversibility include cellular automata [28], bidirectional program transformation [24], already discussed in Section 6, reversible debugging [17], where the ability to go both forward and backward when seeking the cause of an error can be very useful for the programmer, parallel discrete event simulation [34], where reversibility is used to undo the effects of speculative computations made on a wrong assumption, quantum computing [39], where all computations should be reversible, and so forth. The interested reader can find detailed surveys in the state of the art reports of the different working groups of COST Action IC1405 on Reversible Computation [20].

Intuitively speaking, there are two broad approaches to reversibility from a programming language perspective:

Reversible programming languages.

In this case, all constructs of the programming language are reversible. One of the most popular languages within the first approach is the reversible (imperative) language Janus [23]. The language was recently rediscovered [42, 41, 43] and has since been formalized and further developed.

Irreversible programming languages and Landauer’s embedding.

Alternatively, one can consider an irreversible programming language, and enhance the states with some additional information (typically, the history of the computation so far) so that computations become reversible. This is called Landauer’s embedding.

In this work, we consider reversibility in the context of term rewriting. To the best of our knowledge, we have presented the first approach to reversibility in term rewriting. A closest approach was introduced by Abramsky in the context of pattern matching automata [2], though his developments could easily be applied to rewrite systems as well. In Abramsky’s approach, biorthogonality was required to ensure reversibility, which would be a very significant restriction for term rewriting systems. Basically, biorthogonality requires that, for every pair of (different) rewrite rules lr𝑙𝑟l\to r and lrsuperscript𝑙superscript𝑟l^{\prime}\to r^{\prime}, l𝑙l and lsuperscript𝑙l^{\prime} do not overlap (roughly, they do not unify) and r𝑟r and rsuperscript𝑟r^{\prime} do not overlap too. Trivially, the functions of a biorthogonal system are injective and, thus, computations are reversible without the need of a Landauer embedding. Therefore, Abramsky’s work is aimed at defining a reversible language, in contrast to our approach that is based on defining a Landauer embedding for standard term rewriting and a general class of rewrite systems.

Defining a Landauer embedding in order to make a computation mechanism reversible has been applied in different contexts and computational models, e.g., a probabilistic guarded command language [44], a low level virtual machine [35], the call-by-name lambda calculus [19, 21], cellular automata [38, 27], combinatory logic [11], a flowchart language [41], etc.

In the context of declarative languages, we find the work by Mu et al. [29], where a relational reversible language is presented (in the context of bidirectional programming). A similar approach was then introduced by Matsuda et al. [24, 25] in the context of functional programs and bidirectional transformation. The functional programs considered in [24] can be seen as linear and right-treeless191919There are no nested defined symbols in the right-hand sides, and, moreover, any term rooted by a defined function in the right-hand sides can only take different variables as its proper subterms. constructor TRSs. The class of functional programs is more general in [25], which would correspond to left-linear, right-treeless TRSs. The reversibilization technique of [24, 25] includes both an injectivization stage (by introducing a view complement function) and an inversion stage. These methods are closely related to the transformations of injectivization and inversion that we have presented in Section 5, although we developed them from a rather different starting point. Moreover, their methods for injectivization and inversion consider a more restricted class of systems than those considered in this paper. On the other hand, they apply a number of analyses to improve the result, which explains the smaller traces in their approach. All in all, we consider that our approach gives better insights to understand the need for some of the requirements of the program transformations and the class of considered programs. For instance, most of our requirements come from the need to remove programs positions from the traces, as shown in Section 4.

Finally, [37] considers the reversible language RFUN. Similarly to Janus, computations in RFUN are reversible without the need of a Landauer embedding. The paper also presents a transformation from a simple (irreversible) functional language, FUN, to RFUN, in order to highlight how irreversibilities are handled in RFUN. The transformation has some similarities with both the approach of [24] and our improved transformation in Section 5.3; on the other hand, though, [37] also applies the Bennett trick [6] in order to avoid some unnecessary information.

8 Discussion and Future Work

In this paper, we have introduced a reversible extension of term rewriting. In order to keep our approach as general as possible, we have initially considered DCTRSs as input systems, and proved the soundness and reversibility of our extension of rewriting. Then, in order to introduce a reversibilization transformation for these systems, we have also presented a transformation from DCTRSs to pure constructor systems (pcDCTRSs) which is correct for constructor reduction. A further improvement is presented for injective functions, which may have a significant impact in memory usage in some cases. Finally, we have successfully applied our approach in the context of bidirectional program transformation.

We have developed a prototype implementation of the reversibilization transformations introduced in Section 5. The tool can read an input TRS file (format .trs [1]) and then it applies in a sequential way the following transformations: flattening, simplification of constructor conditions, injectivization, and inversion. The tool prints out the CTRSs obtained at each transformation step. It is publicly available through a web interface from http://kaz.dsic.upv.es/rev-rewriting.html, where we have included a number of examples to easily test the tool.

As for future work, we plan to investigate new methods to further reduce the size of the traces. In particular, we find it interesting to define a reachability analysis for DCTRSs. A reachability analysis for CTRSs without extra-variables (1-CTRSs) can be found in [12], but the extension to deal with extra-variables in DCTRSs (since a DCTRS is a particular case of 3-CTRS) seems challenging. Furthermore, as mentioned in the paper, a completion procedure to add default cases to some functions (as suggested in Section 5.1) may help to broaden the applicability of the technique and avoid the restriction to constructor reduction. Finally, our injectivization and inversion transformations are correct w.r.t. innermost reduction. Extending our results to a lazy strategy is also an interesting topic for further research.

Acknowledgements

We thank the anonymous reviewers for their useful comments and suggestions to improve this paper.

References

  • [1] Annual international termination competition. Available from URL:
    http://www.termination-portal.org/wiki/Termination_Competition.
  • [2] S. Abramsky. A structural approach to reversible computation. Theoretical Computer Science, 347(3):441–464, 2005.
  • [3] J. M. Almendros-Jiménez and G. Vidal. Automatic partial inversion of inductively sequential functions. In Z. Horváth, V. Zsók, and A. Butterfield, editors, Implementation and Application of Functional Languages, 18th International Symposium (IFL 2006), Revised Selected Papers, volume 4449 of Lecture Notes in Computer Science, pages 253–270. Springer, 2007.
  • [4] F. Baader and T. Nipkow. Term Rewriting and All That. Cambridge University Press, 1998.
  • [5] F. Bancilhon and N. Spyratos. Update semantics of relational views. ACM Transactions on Database Systems, 6(4):557–575, 1981.
  • [6] C. H. Bennett. Logical reversibility of computation. IBM Journal of Research and Development, 17:525–532, 1973.
  • [7] C. H. Bennett. Notes on the history of reversible computation. IBM Journal of Research and Development, 44(1):270–278, 2000.
  • [8] J. Bergstra and J. Klop. Conditional Rewrite Rules: confluence and termination. Journal of Computer and System Sciences, 32:323–362, 1986.
  • [9] P. Crescenzi and C. H. Papadimitriou. Reversible simulation of space-bounded computations. Theoretical Computer Science, 143(1):159–165, 1995.
  • [10] K. Czarnecki, J. N. Foster, Z. Hu, R. Lämmel, A. Schürr, and J. F. Terwilliger. Bidirectional transformations: A cross-discipline perspective. In R. F. Paige, editor, Proc. of the 2nd Int’l Conf. on Theory and Practice of Model Transformations (ICMT 2009), volume 5563 of Lecture Notes in Computer Science, pages 260–283. Springer, 2009.
  • [11] A. Di Pierro, C. Hankin, and H. Wiklicky. Reversible combinatory logic. Mathematical Structures in Computer Science, 16(4):621–637, 2006.
  • [12] G. Feuillade and T. Genet. Reachability in Conditional Term Rewriting Systems. Electronic Notes in Theoretical Computer Science, 86(1):133–146, 2003.
  • [13] J. N. Foster, M. B. Greenwald, J. T. Moore, B. C. Pierce, and A. Schmitt. Combinators for bidirectional tree transformations: A linguistic approach to the view-update problem. ACM Transactions on Programming Languages and Systems, 29(3):17, 2007.
  • [14] M. P. Frank. Introduction to reversible computing: motivation, progress, and challenges. In N. Bagherzadeh, M. Valero, and A. Ramírez, editors, Proceedings of the Second Conference on Computing Frontiers, pages 385–390. ACM, 2005.
  • [15] T. Genet. Decidable Approximations of Sets of Descendants and Sets of Normal Forms. In T. Nipkow, editor, Proc. of the 9th International Conference on Rewriting Techniques and Applications (RTA’98), volume 1379 of Lecture Notes in Computer Science, pages 151–165. Springer, 1998.
  • [16] T. Genet and Y. Salmon. Reachability Analysis of Innermost Rewriting. In M. Fernández, editor, Proc. of the 26th International Conference on Rewriting Techniques and Applications (RTA’15), volume 36 of LIPIcs, pages 177–193. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, 2015.
  • [17] E. Giachino, I. Lanese, and C. A. Mezzina. Causal-consistent reversible debugging. In S. Gnesi and A. Rensink, editors, Proc. of the 17th International Conference on Fundamental Approaches to Software Engineering (FASE 2014), volume 8411 of Lecture Notes in Computer Science, pages 370–384. Springer, 2014.
  • [18] N. Hirokawa and G. Moser. Automated Complexity Analysis Based on the Dependency Pair Method. In A. Armando, P. Baumgartner, and G. Dowek, editors, Proc. of IJCAR 2008, volume 5195 of Lecture Notes in Computer Science, pages 364–379. Springer, 2008.
  • [19] L. Huelsbergen. A logically reversible evaluator for the call-by-name lambda calculus. In T. Toffoli and M. Biafore, editors, Proc. of PhysComp96, pages 159–167. New England Complex Systems Institute, 1996.
  • [20] COST Action IC1405 on Reversible Computation - extending horizons of computing. URL: http://revcomp.eu/.
  • [21] W. E. Kluge. A reversible SE(M)CD machine. In P. W. M. Koopman and C. Clack, editors, Proc. of the 11th International Workshop on the Implementation of Functional Languages, IFL’99. Selected Papers, volume 1868 of Lecture Notes in Computer Science, pages 95–113. Springer, 2000.
  • [22] R. Landauer. Irreversibility and heat generation in the computing process. IBM Journal of Research and Development, 5:183–191, 1961.
  • [23] C. Lutz and H. Derby. Janus: A time-reversible language, 1986. A letter to R. Landauer. Available from URL http://tetsuo.jp/ref/janus.pdf.
  • [24] K. Matsuda, Z. Hu, K. Nakano, M. Hamana, and M. Takeichi. Bidirectionalization transformation based on automatic derivation of view complement functions. In R. Hinze and N. Ramsey, editors, Proc. of the 12th ACM SIGPLAN International Conference on Functional Programming, ICFP 2007, pages 47–58. ACM, 2007.
  • [25] K. Matsuda, Z. Hu, K. Nakano, M. Hamana, and M. Takeichi. Bidirectionalizing programs with duplication through complementary function derivation. Computer Software, 26(2):56–75, 2009. In Japanese.
  • [26] A. Middeldorp and E. Hamoen. Completeness results for basic narrowing. Applicable Algebra in Engineering, Communication and Computing, 5:213–253, 1994.
  • [27] K. Morita. Reversible simulation of one-dimensional irreversible cellular automata. Theoretical Computer Science, 148(1):157–163, 1995.
  • [28] K. Morita. Computation in reversible cellular automata. International Journal of General Systems, 41(6):569–581, 2012.
  • [29] S. Mu, Z. Hu, and M. Takeichi. An injective language for reversible computation. In D. Kozen and C. Shankland, editors, Proc. of the 7th International Conference on Mathematics of Program Construction (MPC 2004), volume 3125 of Lecture Notes in Computer Science, pages 289–313. Springer, 2004.
  • [30] M. Nagashima, M. Sakai, and T. Sakabe. Determinization of conditional term rewriting systems. Theoretical Computer Science, 464:72–89, 2012.
  • [31] N. Nishida, A. Palacios, and G. Vidal. Reversible term rewriting. In D. Kesner and B. Pientka, editors, Proc. of the 1st International Conference on Formal Structures for Computation and Deduction (FSCD’16), volume 52 of LIPIcs, pages 28:1–28:18. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, 2016.
  • [32] N. Nishida, M. Sakai, and T. Sakabe. Soundness of unravelings for conditional term rewriting systems via ultra-properties related to linearity. Logical Methods in Computer Science, 8(3-4):1–49, Aug. 2012.
  • [33] N. Nishida and G. Vidal. Program inversion for tail recursive functions. In M. Schmidt-Schauß, editor, Proceedings of the 22nd International Conference on Rewriting Techniques and Applications (RTA 2011), volume 10 of LIPIcs, pages 283–298. Schloss Dagstuhl - Leibniz-Zentrum fuer Informatik, 2011.
  • [34] M. Schordan, D. R. Jefferson, P. D. B. Jr., T. Oppelstrup, and D. J. Quinlan. Reverse code generation for parallel discrete event simulation. In J. Krivine and J. Stefani, editors, Proc. of the 7th International Conference on Reversible Computation (RC 2015), volume 9138 of Lecture Notes in Computer Science, pages 95–110. Springer, 2015.
  • [35] B. Stoddart, R. Lynas, and F. Zeyda. A virtual machine for supporting reversible probabilistic guarded command languages. Electronic Notes in Theoretical Computer Science, 253(6):33–56, 2010.
  • [36] Terese. Term Rewriting Systems, volume 55 of Cambridge Tracts in Theoretical Computer Science. Cambridge University Press, 2003.
  • [37] M. K. Thomsen and H. B. Axelsen. Interpretation and programming of the reversible functional language RFUN. In R. Lämmel, editor, Proc. of the 27th Symposium on the Implementation and Application of Functional Programming Languages (IFL’15), pages 8:1–8:13. ACM, 2015.
  • [38] T. Toffoli. Computation and construction universality of reversible cellular automata. Journal of Computer and System Sciences, 15(2):213–231, 1977.
  • [39] T. Yamakami. One-way reversible and quantum finite automata with advice. Information and Computation, 239:122–148, 2014.
  • [40] T. Yokoyama. Reversible computation and reversible programming languages. Electronic Notes in Theoretical Computer Science, 253(6):71–81, 2010.
  • [41] T. Yokoyama, H. Axelsen, and R. Glück. Fundamentals of reversible flowchart languages. Theoretical Computer Science, 611:87–115, 2016.
  • [42] T. Yokoyama, H. B. Axelsen, and R. Glück. Reversible flowchart languages and the structured reversible program theorem. In Proc. of the 35th International Colloquium on Automata, Languages and Programming (ICALP 2008), volume 5126 of Lecture Notes in Computer Science, pages 258–270. Springer, 2008.
  • [43] T. Yokoyama and R. Glück. A reversible programming language and its invertible self-interpreter. In G. Ramalingam and E. Visser, editors, Proc. of the 2007 ACM SIGPLAN Workshop on Partial Evaluation and Semantics-based Program Manipulation (PEPM 2007), pages 144–153. ACM, 2007.
  • [44] P. Zuliani. Logical reversibility. IBM Journal of Research and Development, 45(6):807–818, 2001.